我想把下面的字符串拆分成单词'and‘,除非单词'and’在引号内
string = "section_category_name = 'computer and equipment expense' and date >= 2015-01-01 and date <= 2015-03-31"期望结果
["section_category_name = 'computer and equipment expense'","date >= 2015-01-01","date <= 2015-03-31"]我似乎找不到正确的正则表达式模式,它正确地拆分了字符串,因此“计算机和设备费用”不会被分割。
以下是我尝试过的:
re.split('and',string)结果
[" section_category_name = 'computer "," equipment expense' ",' date >= 2015-01-01 ',' date <= 2015-03-31']如您所见,结果将“计算机和设备费用”拆分为列表中的不同项目。
我还在这个问题上尝试了以下几种方法:
r = re.compile('(?! )[^[]+?(?= *\[)'
'|'
'\[.+?\]')
r.findall(s)结果:
[]我还从这个问题中尝试了以下内容
result = re.split(r"and+(?=[^()]*(?:\(|$))", string)结果:
[" section_category_name = 'computer ",
" equipment expense' ",
' date >= 2015-01-01 ',
' date <= 2015-03-31']目前的挑战是,有关此主题的先前问题并不涉及如何用引号中的一个单词拆分字符串,因为它们解决了如何用特殊字符或空格拆分字符串。
如果我将字符串修改为以下内容,我就能够得到所需的结果
string = " section_category_name = (computer and equipment expense) and date >= 2015-01-01 and date <= 2015-03-31"
result = re.split(r"and+(?=[^()]*(?:\(|$))", string)期望结果
[' section_category_name = (computer and equipment expense) ',
' date >= 2015-01-01 ',
' date <= 2015-03-31']但是,我需要函数在撇号中不拆分‘和’,而不是括号。
发布于 2015-12-23 22:25:14
您可以在re.findall中使用以下正则表达式
((?:(?!\band\b)[^'])*(?:'[^'\\]*(?:\\.[^'\\]*)*'(?:(?!\band\b)[^'])*)*)(?:and|$)见regex演示。
正则表达式由一个未包装的序列组成,其中除了一个',直到第一个and (带有经过调整的贪婪令牌(?:(?!\band\b)[^'])*)和任何东西(支持转义实体)在单个撇号之间,并包括单个撇号( '[^'\\]*(?:\\.[^'\\]*)*' --这也是([^'\\]|\\.)*的未包装版本)。
Python 代码演示
import re
p = re.compile(r'((?:(?!\band\b)[^\'])*(?:\'[^\'\\]*(?:\\.[^\'\\]*)*\'(?:(?!\band\b)[^\'])*)*)(?:and|$)')
s = "section_category_name = 'computer and equipment expense' and date >= 2015-01-01 and date <= 2015-03-31"
print([x for x in p.findall(s) if x])发布于 2015-12-24 00:44:26
您可以使用re.findall生成一个二元元组的列表,其中第一个元素要么是带引号的字符串,要么是空的,或者第二个元素是除空格字符或空以外的任何东西。
然后,您可以使用itertools.groupby按单词"and“进行分区(如果不是在引用的字符串中),然后从list-comp中的填充元素重新连接,例如:
import re
from itertools import groupby
text = "section_category_name = 'computer and equipment expense' and date >= 2015-01-01 and date <= 2015-03-31 and blah = 'ooops'"
items = [
' '.join(el[0] or el[1] for el in g)
for k, g in groupby(re.findall("('.*?')|(\S+)", text), lambda L: L[1] == 'and')
if not k
]给你:
["section_category_name = 'computer and equipment expense'",
'date >= 2015-01-01',
'date <= 2015-03-31',
"blah = 'ooops'"]注意,空格在引用字符串之外也是标准化的--不管这是否可取,尽管.
另外请注意-这确实允许在分组中有一定的灵活性,因此您可以将lambda L: L[1] == 'and'更改为lambda L: L[1] in ('and', 'or'),如果需要的话,可以将不同的单词分组。
发布于 2015-12-23 22:44:13
如果您的所有字符串都遵循相同的模式,则可以使用regex将其划分为三个组。第一组从开始到最后‘。然后下一组是第一个和最后一个“和”之间的所有东西。最后一组是课文的其余部分。
import re
string = "section_category_name = 'computer and equipment expense' and date >= 2015-01-01 and date <= 2015-03-31"
output = re.match(r"(^.+['].+['])\sand\s(.+)\sand\s(.+)", string).groups()
print(output)每个组都在正则表达式中的括号中定义。方括号定义了要匹配的特定字符。只有当"section_category_name“等于单引号中的某个内容时,此示例才能工作。
section_category_name = 'something here' and ...https://stackoverflow.com/questions/34444319
复制相似问题