简体   繁体   English

如何在包含两个单词的引号中存储字符串?

[英]How to store string in quotation that contains two words?

I wrote the search code and I want to store what is between " " as one place in the list, how I may do that?我编写了搜索代码,我想将“”之间的内容存储为列表中的一个位置,我该怎么做? In this case, I have 3 lists but the second one should is not as I want.在这种情况下,我有 3 个列表,但第二个should不是我想要的。

import re

message='read read read'

others = ' '.join(re.split('\(.*\)', message))
others_split = others.split()

to_compile = re.compile('.*\((.*)\).*')
to_match = to_compile.match(message)
ors_string = to_match.group(1)

should = ors_string.split(' ')

must = [term for term in re.findall(r'\(.*?\)|(-?(?:".*?"|\w+))', message) if term and not term.startswith('-')]

must_not = [term for term in re.findall(r'\(.*?\)|(-?(?:".*?"|\w+))', message) if term and term.startswith('-')]
must_not = [s.replace("-", "") for s in must_not]

print(f'must: {must}')
print(f'should: {should}')
print(f'must_not: {must_not}')

Output:输出:

must: ['read', '"find find"', 'within', '"plane"']
should: ['"exactly', 'needed"', 'empty']
must_not: ['russia', '"destination good"']

Wanted result :想要的结果

must: ['read', '"find find"', 'within', '"plane"']
should: ['"exactly needed"', 'empty'] <---
must_not: ['russia', '"destination good"']

Error when edited the message, how to handle it?编辑消息时出错,如何处理?

Traceback (most recent call last):
    ors_string = to_match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Your should list splits on whitespace: should = ors_string.split(' ') , this is why the word is split in the list.should在空格上列出拆分: should = ors_string.split(' ') ,这就是单词在列表中拆分的原因。 The following code gives you the output you requested but I'm not sure that is solves your problem for future inputs.以下代码为您提供了您请求的输出,但我不确定这是否可以解决您未来输入的问题。

import re

message = 'read "find find":within("exactly needed" OR empty) "plane" -russia -"destination good"'

others = ' '.join(re.split('\(.*\)', message))
others_split = others.split()

to_compile = re.compile('.*\((.*)\).*')
to_match = to_compile.match(message)
ors_string = to_match.group(1)

# Split on OR instead of whitespace.
should = ors_string.split('OR')
to_remove_or = "OR"
while to_remove_or in should:
    should.remove(to_remove_or)

# Remove trailing whitespace that is left after the split.
should = [word.strip() for word in should]

must = [term for term in re.findall(r'\(.*?\)|(-?(?:".*?"|\w+))', message) if term and not term.startswith('-')]

must_not = [term for term in re.findall(r'\(.*?\)|(-?(?:".*?"|\w+))', message) if term and term.startswith('-')]
must_not = [s.replace("-", "") for s in must_not]

print(f'must: {must}')
print(f'should: {should}')
print(f'must_not: {must_not}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM