RegExp help

MRAB python at mrabarnett.plus.com
Wed Feb 10 22:00:05 EST 2016


On 2016-02-11 02:48, Larry Martell wrote:
> Given this string:
>
>>>> s = """|Type=Foo
> ... |Side=Left"""
>>>> print s
> |Type=Foo
> |Side=Left
>
> I can match with this:
>
>>>> m = re.search(r'^\|Type=(.*)$\n^\|Side=(.*)$',s,re.MULTILINE)
>>>> print m.group(0)
> |Type=Foo
> |Side=Left
>>>> print m.group(1)
> Foo
>>>> print m.group(2)
> Left
>
> But when I try and sub it doesn't work:
>
>>>> rn = re.sub(r'^\|Type=(.*)$^\|Side=(.*)$', r'|Side Type=\2 \1',s,re.MULTILINE)
>>>> print rn
> |Type=Foo
> |Side=Left
>
> What very stupid thing am I doing wrong?
>
The 4th argument of re.sub is the count.




More information about the Python-list mailing list