2

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

6 Answers 6

2

Code it like this:

x = "--x123-09827--x456-9908872--x789-267504"
p = "--x(?:[0-9]+)-(?:[0-9]+)"
print re.findall(p,x)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you and thank all who answer this question, it seems I have to use findall to trigger the repeat.
1

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

Comments

1

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

Comments

1

try this

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

Comments

0

No need to use regex :

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

1 Comment

it works on my example, but no very flexible to similar problem.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.