简体   繁体   English

在Python中实现preg_match_all

[英]Implementing preg_match_all in Python

I basically want the same functionality of preg_match_all() from PHP in a Python way. 我基本上希望以Python的方式从PHP获得与preg_match_all()相同的功能。

If I have a regex pattern and a string, is there a way to search the string and get back a dictionary of each occurrence of a vowel, along with its position in the string? 如果我有一个正则表达式模式和一个字符串,有没有办法搜索字符串并找回每个元音出现的字典,以及它在字符串中的位置?

Example: 例:

s = "supercalifragilisticexpialidocious"

Would return: 会回来:

{
  'u' : 1,
  'e' : 3,
  'a' : 6,
  'i' : 8,
  'a' : 11,
  'i' : 13,
  'i' : 15
}

You can do this faster without regexp 如果没有正则表达式,您可以更快地完成此操作

[(x,i) for i,x in enumerate(s) if x in "aeiou"]

Here are some timings: 以下是一些时间安排:
For s = "supercalifragilisticexpialidocious" 对于s = "supercalifragilisticexpialidocious"

timeit [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
10000 loops, best of 3: 27.5 µs per loop

timeit [(x,i) for i,x in enumerate(s) if x in "aeiou"]
100000 loops, best of 3: 14.4 µs per loop

For s = "supercalifragilisticexpialidocious"*100 对于s = "supercalifragilisticexpialidocious"*100

timeit [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
100 loops, best of 3: 2.01 ms per loop

timeit [(x,i) for i,x in enumerate(s) if x in "aeiou"]
1000 loops, best of 3: 1.24 ms per loop

What you ask for can't be a dictionary, since it has multiple identical keys. 你要求的不能是字典,因为它有多个相同的键。 However, you can put it in a list of tuples like this: 但是,您可以将它放在这样的元组列表中:

>>> [(m.group(0), m.start()) for m in re.finditer('[aeiou]',s)]
[('u', 1), ('e', 3), ('a', 6), ('i', 8), ('a', 11), ('i', 13), ('i', 15), ('i', 18), ('e', 20), ('i', 23), ('a', 24), ('i', 26), ('o', 28), ('i', 30), ('o', 31), ('u', 32)]

Like this, for example: 像这样,例如:

import re

def findall(pattern, string):
    res = {}
    for match in re.finditer(pattern, string):
        res[match.group(0)] = match.start()
    return res

print findall("[aeiou]", "Test this thang")

Note that re.finditer only finds non-overlapping matches. 请注意, re.finditer仅查找非重叠匹配项。 And the dict keys will be overwritten, so if you want the first match, you'll have to replace the innermost loop by: 并且dict键将被覆盖,因此如果你想要第一个匹配,你将必须通过以下方式替换最里面的循环:

    for match in re.finditer(pattern, string):
        if match.group(0) not in res: # <-- don't overwrite key
            res[match.group(0)] = match.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM