Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式匹配有效词在开始、结束和句子中间_Python_Regex - Fatal编程技术网

Python 正则表达式匹配有效词在开始、结束和句子中间

Python 正则表达式匹配有效词在开始、结束和句子中间,python,regex,Python,Regex,我对正则表达式有一个特别的问题。考虑有效词的这个句子: sphere_a [sS]phere_b [sS]pher* [sS]pher* sph[eE]* sphere_a ^sphe* ^sp[hH]er* 我想把这些单词分开,这样我就可以在下游操作中分别使用它们了。为此,我目前使用2个正则表达式。 与句子开头的单词匹配的单词: (?<=^)(?P<pattern>[\w\^\?\*\[\]]+)(?=\s|$) 我正在使用Pythons模块进行验证。您可以使用 reg

我对正则表达式有一个特别的问题。考虑有效词的这个句子:

sphere_a [sS]phere_b [sS]pher* [sS]pher* sph[eE]* sphere_a ^sphe* ^sp[hH]er*
我想把这些单词分开,这样我就可以在下游操作中分别使用它们了。为此,我目前使用2个正则表达式。

与句子开头的单词匹配的单词:

(?<=^)(?P<pattern>[\w\^\?\*\[\]]+)(?=\s|$)


我正在使用Pythons模块进行验证。

您可以使用

regexs = 'sphere_a [sS]phere_b [sS]pher* [sS]pher* sph[eE]* sphere_a ^sphe* ^sp[hH]er*'.split(). 
然后您可以像这样迭代模式:

for regex in regexs:
    m = re.findall(regex, content)

但它会返回重复的匹配。

我相信您的拆分方法可能比使用正则表达式更实用!接受-谢谢!
Invalid regular expression: look-behind requires fixed-width pattern
regexs = 'sphere_a [sS]phere_b [sS]pher* [sS]pher* sph[eE]* sphere_a ^sphe* ^sp[hH]er*'.split(). 
for regex in regexs:
    m = re.findall(regex, content)