Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102781

Re: RegExp help

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: RegExp help
Date 2016-02-11 03:31 +0000
Message-ID <mailman.28.1455161493.22075.python-list@python.org> (permalink)
References <CACwCsY6Lpt2DqwHz5bm2BV-aJmDbTvnv2kMVwq-W0mA1rcpQGw@mail.gmail.com> <56BBF935.9050902@mrabarnett.plus.com> <CACwCsY7qLgF_W-nR6tp=pavwVYGxV0w3x6ZKjUR7D6SZ0S5jvw@mail.gmail.com>

Show all headers | View raw


On 2016-02-11 03:09, Larry Martell wrote:
> On Wed, Feb 10, 2016 at 10:00 PM, MRAB <python@mrabarnett.plus.com> wrote:
>> 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.
>
>
> Thanks. Turned out that this site is running 2.6 and that doesn't
> support the flags arg to sub. So I had to change it to:
>
> re.sub(r'\|Type=(.*)\n\|Side=(.*)', r'\|Side Type=\2 \1',s)
>
You could've used the inline flag "(?m)" in the pattern:

   rn = re.sub(r'(?m)^\|Type=(.*)$^\|Side=(.*)$', r'|Side Type=\2 \1',s)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: RegExp help MRAB <python@mrabarnett.plus.com> - 2016-02-11 03:31 +0000

csiph-web