Python正则表达式行为
我对python正则表达式有一个问题。在regxebuddy上,结果似乎不错,但在python中失败了 我必须匹配的数据集是一个列表:Python正则表达式行为,python,regex,Python,Regex,我对python正则表达式有一个问题。在regxebuddy上,结果似乎不错,但在python中失败了 我必须匹配的数据集是一个列表: [' 101 0. 0.\n', ' 0. 100.\n', ' 1. 98.5107805\n', ' 2. 97.0464459\n', ' 3. 95.6065328\n', ... ] 我必须从第二行开始得到所有的数字。 为此,我使用了: pattern = compile(r'\s*(?P<raw_time>
[' 101 0. 0.\n',
' 0. 100.\n',
' 1. 98.5107805\n',
' 2. 97.0464459\n',
' 3. 95.6065328\n', ... ]
我必须从第二行开始得到所有的数字。
为此,我使用了:
pattern = compile(r'\s*(?P<raw_time>\d*\.?\d*)\s+(?P<raw_value>\d*\.\d*)')
我仍然需要解析出前两个参数。因此,我将模式更改为:
pattern = compile(r'\s*(?P<raw_time>\d*\.?\d*)\s+(?P<raw_value>\d*\.\d*).+')
工作:
In [35]: pattern.search('1. 98.5107805 1\n').groupdict()
Out[35]: {'raw_time': '1.', 'raw_value': '98.5107805'}
In [37]: pattern.search(' 0. 100. 1\n').groupdict()
Out[37]: {'raw_time': '0.', 'raw_value': '100.'}
工作:
In [35]: pattern.search('1. 98.5107805 1\n').groupdict()
Out[35]: {'raw_time': '1.', 'raw_value': '98.5107805'}
In [37]: pattern.search(' 0. 100. 1\n').groupdict()
Out[37]: {'raw_time': '0.', 'raw_value': '100.'}
[44]中的pattern.search('1.98.5107805\n').groupdict()
Out[44]:{'raw_time':'1','raw_value':'98.510780'}
不工作:
In [46]: pattern.search(' 0. 100.\n').groupdict()
Out[46]: {'raw_time': '', 'raw_value': '0.'}
我非常依赖正则表达式(当然是demo,但到目前为止它与python是一致的)
建议
tnx您添加的
+
需要更改为*
+
运算符至少需要一个字符,而*
将接受零个或多个字符。请清楚地显示它不工作的输入(就像您在工作示例中所做的那样)。谢谢。Reggex buddy不会帮你使用Python正则表达式。您需要一个Python正则表达式测试仪,如: