简体   繁体   English

Python正则表达式重复

[英]Python Regular expression repeat

I have a string like this 我有这样的字符串

--x123-09827--x456-9908872--x789-267504 --x123-09827 - x456-9908872 - x789-267504

I am trying to get all value like 123:09827 456:9908872 789:267504 我试图获得所有价值,例如123:09827 456:9908872 789:267504

I've tried (--x([0-9]+)-([0-9])+)+ 我已经尝试过(--x([0-9] +)-([0-9])+)+

but it only gives me last pair result, I am testing it through python 但这只给了我最后一对结果,我正在通过python测试它

>>> import re
>>> x = "--x123-09827--x456-9908872--x789-267504"
>>> p = "(--x([0-9]+)-([0-9]+))+"
>>> re.match(p,x)
>>> re.match(p,x).groups()
('--x789-267504', '789', '267504')

How should I write with nested repeat pattern? 我应该如何使用嵌套重复模式书写?

Thanks a lot! 非常感谢!

David 大卫

Code it like this: 像这样编码:

x = "--x123-09827--x456-9908872--x789-267504"
p = "--x(?:[0-9]+)-(?:[0-9]+)"
print re.findall(p,x)

try this 尝试这个

p='--x([0-9]+)-([0-9]+)'
re.findall(p,x)

Just use the .findall method instead, it makes the expression simpler. 只需使用.findall方法即可,它使表达式更简单。

>>> import re
>>> x = "--x123-09827--x456-9908872--x789-267504"
>>> r = re.compile(r"--x(\d+)-(\d+)")
>>> r.findall(x)
[('123', '09827'), ('456', '9908872'), ('789', '267504')]

You can also use .finditer which might be helpful for longer strings. 您也可以使用.finditer ,这可能对较长的字符串有用。

>>> [m.groups() for m in r.finditer(x)]
[('123', '09827'), ('456', '9908872'), ('789', '267504')]

Use re.finditer or re.findall. 使用re.finditer或re.findall。 Then you don't need the extra pair of parentheses that wrap the entire expression. 这样,您就不需要多余的括号来包裹整个表达式了。 For example, 例如,

    >>> import re
    >>> x = "--x123-09827--x456-9908872--x789-267504"
    >>> p = "--x([0-9]+)-([0-9]+)"
    >>> for m in re.finditer(p,x):
    >>>    print '{0} {1}'.format(m.group(1),m.group(2))

No need to use regex : 无需使用正则表达式:

>>> "--x123-09827--x456-9908872--x789-267504".replace('--x',' ').replace('-',':').strip()
'123:09827 456:9908872 789:267504'

You don't need regular expressions for this. 您不需要为此使用正则表达式。 Here is a simple one-liner, non-regex solution: 这是一个简单的单行非正则表达式解决方案:

>>> input = "--x123-09827--x456-9908872--x789-267504"
>>> [ x.replace("-", ":") for x in input.split("--x")[1:] ]
['123:09827', '456:9908872', '789:267504']

If this is an exercise on regex, here is a solution that uses the repetition (technically), though the findall(...) solution may be preferred: 如果这是在正则表达式上进行的练习,则以下是一个使用重复(技术上)的解决方案,尽管findall(...)解决方案可能是首选:

>>> import re
>>> input = "--x123-09827--x456-9908872--x789-267504"
>>> regex = '--x(.+)'
>>> [ x.replace("-", ":") for x in re.match(regex*3, input).groups() ]
['123:09827', '456:9908872', '789:267504']

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

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