Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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_Text Processing - Fatal编程技术网

Python 如何捕获特定角色前后的所有组

Python 如何捕获特定角色前后的所有组,python,regex,text-processing,Python,Regex,Text Processing,我正在尝试捕获以前的所有组;。我还需要捕获最后一个未以;结束的组;。这是我的声明和代码 正则表达式: ((\*|\/|\)|\(|[-+]\d+|[-+]?\d*\.\d+|\d+|\w+d?|\+|\-|=|{|}|:=|while|do|if|else|then|skip|or|and|not|>=)+;)+ 声明: x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2;

我正在尝试捕获以前的所有组;。我还需要捕获最后一个未以;结束的组;。这是我的声明和代码

正则表达式:

((\*|\/|\)|\(|[-+]\d+|[-+]?\d*\.\d+|\d+|\w+d?|\+|\-|=|{|}|:=|while|do|if|else|then|skip|or|and|not|>=)+;)+
声明:

x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2; x2:=x3; x3:= (x1, x2,+)}
我的正则表达式只捕获第一组。我需要捕获所有组,包括最后一组

因此,最后一组应该是:

['x1:=0', 'x2:=1', 'x3:= (x1,x2,+)', 'x4:=5', 'while {(x4,0,>=)} do {x4:= (x4,1,-)', 'x1:=x2', 'x2:=x3', 'x3:= (x1, x2,+)']

看起来您可以使用拆分:

ting = 'x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2; x2:=x3; x3:= (x1, x2,+)}'
ting2 = ting.split(';')
# ['x1:=0', ' x2:=1', ' x3:= (x1,x2,+)', ' x4:=5', ' while {(x4,0,>=)} do {x4:= (x4,1,-)', ' x1:=x2', ' x2:=x3', ' x3:= (x1, x2,+)}']

看起来您可以使用拆分:

ting = 'x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2; x2:=x3; x3:= (x1, x2,+)}'
ting2 = ting.split(';')
# ['x1:=0', ' x2:=1', ' x3:= (x1,x2,+)', ' x4:=5', ' while {(x4,0,>=)} do {x4:= (x4,1,-)', ' x1:=x2', ' x2:=x3', ' x3:= (x1, x2,+)}']

这有两种非常简单的方法。一个人甚至不需要正则表达式。下面的代码显示了两种不同的实现。您想要的模式是:

“?[^;]+;?”

示例代码:

import re

statement = 'x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2; x2:=x3; x3:= (x1, x2,+)}'

#-the quick way
print('Quick way:')
print(state.split('; '))

#-the ~magic~ regex way
print('Regex way:')
pattern = ' ?([^;]+);?'
print(re.compile(pat).findall(state))
输出:

Quick way:
['x1:=0', 'x2:=1', 'x3:= (x1,x2,+)', 'x4:=5', 'while {(x4,0,>=)} do {x4:= (x4,1,-)', 'x1:=x2', 'x2:=x3', 'x3:= (x1, x2,+)}']
Regex way:
['x1:=0', 'x2:=1', 'x3:= (x1,x2,+)', 'x4:=5', 'while {(x4,0,>=)} do {x4:= (x4,1,-)', 'x1:=x2', 'x2:=x3', 'x3:= (x1, x2,+)}']

这有两种非常简单的方法。一个人甚至不需要正则表达式。下面的代码显示了两种不同的实现。您想要的模式是:

“?[^;]+;?”

示例代码:

import re

statement = 'x1:=0; x2:=1; x3:= (x1,x2,+); x4:=5; while {(x4,0,>=)} do {x4:= (x4,1,-); x1:=x2; x2:=x3; x3:= (x1, x2,+)}'

#-the quick way
print('Quick way:')
print(state.split('; '))

#-the ~magic~ regex way
print('Regex way:')
pattern = ' ?([^;]+);?'
print(re.compile(pat).findall(state))
输出:

Quick way:
['x1:=0', 'x2:=1', 'x3:= (x1,x2,+)', 'x4:=5', 'while {(x4,0,>=)} do {x4:= (x4,1,-)', 'x1:=x2', 'x2:=x3', 'x3:= (x1, x2,+)}']
Regex way:
['x1:=0', 'x2:=1', 'x3:= (x1,x2,+)', 'x4:=5', 'while {(x4,0,>=)} do {x4:= (x4,1,-)', 'x1:=x2', 'x2:=x3', 'x3:= (x1, x2,+)}']

谢谢!这比我做的更符合逻辑。在我从分号中分离出来后,我使用我的正则表达式的修改版本一个接一个地获取所有字符。非常感谢!这比我做的更符合逻辑。在我从分号中分离出来之后,我使用我的正则表达式的修改版本一个接一个地获取所有字符。