Python正则表达式搜索

Python正则表达式搜索,python,regex,Python,Regex,我想使用正则表达式在文件中搜索此表达式: time:<float> s 嗯,我从m.group(0) 我怎样才能直接得到1.5?我想你真正想要的正则表达式更像: re.compile(r'time:(\d+\.\d+)') 甚至: re.compile(r'time:(\d+(?:\.\d+)?)') # This one will capture integers too. 请注意,我已将全部时间分为1组。我还转义了,它表示正则表达式中的任何字符 然后,您将从m.group

我想使用正则表达式在文件中搜索此表达式:

time:<float> s
嗯,我从
m.group(0)

我怎样才能直接得到
1.5

我想你真正想要的正则表达式更像:

re.compile(r'time:(\d+\.\d+)')
甚至:

re.compile(r'time:(\d+(?:\.\d+)?)')  # This one will capture integers too.
请注意,我已将全部时间分为1组。我还转义了
,它表示正则表达式中的任何字符

然后,您将从
m.group(1)
--
m.group(0)
获得
1.5
m.group(1)
是第一个子匹配(括号中的分组),
m.group(2)
是第二个分组,以此类推

例如:

>>> import re
>>> p = re.compile(r'time:(\d+(?:\.\d+)?)')
>>> p.search('time:34')
<_sre.SRE_Match object at 0x10fa77d50>
>>> p.search('time:34').group(1)
'34'
>>> p.search('time:34.55').group(1)
'34.55'
>>重新导入
>>>p=re.compile(r'time:(\d+(?:\。\d+)))
>>>p.search('时间:34')
>>>p.search('时间:34')。组(1)
'34'
>>>p.search('time:34.55')。组(1)
'34.55'

您可以为此创建另一个组。我还将稍微更改正则表达式,以允许没有小数分隔符的数字

re.compile(r'time:((\d+)(\.?(\d+))?')

现在,您可以使用
组(1)
来捕获浮点数的匹配。

因为您说您正在学习正则表达式,所以我将包括一些额外的python专用资料。如前所述,在如下所述的各种命令中,最简单的正则表达式肯定是
\d+\.\d+

一开始,让我对python不感兴趣的是各种re方法的返回类型以及何时使用group()和groups()

您可以使用以下几种方法:

  • 关于match()
  • 检索()
  • 关于findall()
match()
仅当在字符串开头找到模式时才会返回对象。
search()
将查找第一个图案和顶部。
findall()
将查找字符串中的所有内容

match()和search()的返回类型为match对象,\uuu match[T],如果未找到匹配项,则为None。但是,findall()的返回类型是一个列表[T]。这些不同的返回类型显然对如何从匹配中获取值有影响

match和search都公开了用于检索匹配项的group()和groups()方法。但是当使用findall时,您需要遍历列表或使用枚举器提取值。因此,使用findall:

>>>import re
>>>easy = re.compile(r'123')
>>>matches = easy.findall(search_me)
>>>for match in matches: print match
123
如果使用的是search()或match(),则需要使用.group()或groups()来检索匹配项,具体取决于正则表达式的设置方式

从中,“groups()方法返回一个元组,该元组包含所有子组的字符串,从1到有多少子组。”

因此,如果您的正则表达式中没有组,如以下示例所示,您将无法得到任何回报:

>>>import re
>>>search_me = '123abc'
>>>easy = re.compile(r'123')
>>>matches = easy.search(search_me)
>>>print matches.groups()
()
将“组”添加到正则表达式可以使用以下功能:

>>>import re
>>>search_me = '123abc'
>>>easy = re.compile(r'(123)')
>>>matches = easy.search(search_me)
>>>print matches.groups()
('123',)
您不必在正则表达式中指定组。group(0)或group()将返回整个匹配项,即使表达式中括号中没有任何内容--group()默认为组(0)

如果使用括号,则可以使用“组”来匹配特定的组和子组

>>>import re
>>>search_me = '123abc'
>>>easy = re.compile(r'((1)(2)(3))')
>>>matches = easy.search(search_me)
>>>print matches.group(1)
>>>print matches.group(2)
>>>print matches.group(3)
>>>print matches.group(4)
123
1
2
3
我还想指出,除非出于可用性和/或可读性的考虑,否则您不必编译正则表达式。这不会提高你的表现

>>>import re
>>>search_me = '123abc'
>>>#easy = re.compile(r'123')
>>>#matches = easy.search(search_me)
>>>matches = re.search(r'123', search_me)
>>>print matches.group()
希望这有帮助!我发现像debuggex这样的网站在学习正则表达式时很有用。(虽然有时你必须刷新那些页面;一天晚上我在脑袋上敲了几个小时才意识到在重新加载页面后,我的正则表达式工作得很好。)最近,我认为你也可以通过将沙盒代码扔进像wakari.io或像PyCharm这样的IDE中,并观察输出结果。也是了解一般正则表达式知识的好网站

>>>import re
>>>search_me = '123abc'
>>>easy = re.compile(r'((1)(2)(3))')
>>>matches = easy.search(search_me)
>>>print matches.group(1)
>>>print matches.group(2)
>>>print matches.group(3)
>>>print matches.group(4)
123
1
2
3
>>>import re
>>>search_me = '123abc'
>>>#easy = re.compile(r'123')
>>>#matches = easy.search(search_me)
>>>matches = re.search(r'123', search_me)
>>>print matches.group()