Prev: Is there any way to minimize str()/unicode() objects memory usage [Python 2.6.4] ?
Next: Sequential Object Store
From: Peng Yu on 7 Aug 2010 11:48 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. -- Regards, Peng
From: Alex Willmer on 7 Aug 2010 12:15 On Aug 7, 4:48 pm, Peng Yu <pengyu...(a)gmail.com> wrote: > 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. >>> import re >>> re.match(r'test\((\w+) (\w+)\)\1 \2', 'test(xy uv)xy uv').groups() ('xy', 'uv') >>> re.match(r'test\((\w+) (\w+)\)\1 \2', 'test(a b)x y') >>>
From: MRAB on 7 Aug 2010 14:20
Peng Yu wrote: > 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. > Use backreferences (it's in the documentation): pattern = re.compile(r'test\((\w+) (\w+)\)\1 \2') |