简体   繁体   中英

Python Regular expression repeat

I have a string like this

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

I am trying to get all value like 123:09827 456:9908872 789:267504

I've tried (--x([0-9]+)-([0-9])+)+

but it only gives me last pair result, I am testing it through 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.

>>> 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.

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

Use re.finditer or 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:

>>> 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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