How to match patterns like XX YY XX YY? (regex)

P

Peng Yu

Hi,

Suppose that I have strings like the following

test(a b)a b
test(xy uv)xy uv
....

I want to change them to

test(a)a test(b)b
test(xy)xy test(uv)uv
....


The problem is that I don't know how to capture pattern that repeat
itself (like 'a' and 'xy' in the example). I could use 'test\((\w+)
(\w+)\)(\w) (\w)', but it will capture something like 'test(a b)x y',
which I don't want to.

I'm wondering if there is way to capture recurring patterns.
 
A

Alex Willmer

The problem is that I don't know how to capture pattern that repeat
itself (like 'a' and 'xy' in the example). I could use 'test\((\w+)
(\w+)\)(\w) (\w)', but it will capture something like 'test(a b)x y',
which I don't want to.

I'm wondering if there is way to capture recurring patterns.

Back references can deal with repetition.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top