[Tutor] Regular Expression Misunderstanding
Kent Johnson
kent37 at tds.net
Fri Jul 14 15:35:26 CEST 2006
Steve Nelson wrote:
> Incidentally continuing my reading of the HOWTO I have sat and puzzled
> for about 30 mins on the difference the MULTILINE flag makes. I can't
> quite see the difference. I *think* it is as follows:
>
> Under normal circumstances, ^ matches the start of a line, only. On a
> line by line basis.
>
> With the re.M flag, we get a match after *any* newline?
>
> Similarly with $ - under normal circumstances, $ matches the end of
> the string, or that which precedes a newline.
>
> With the MULTILINE flag, $ matches before *any* newline?
>
> Is this correct?
I'm not sure, I think you are a little confused. MULTILINE only matters
if the string you are matching contains newlines. Without MULTILINE, ^
will match only at the beginning of the string. With it, ^ will match
after any newline. For example,
In [1]: import re
A string containing two lines:
In [2]: s='one\ntwo'
The first line matches without MULTILINE:
In [3]: re.search('^one', s)
Out[3]: <_sre.SRE_Match object at 0x00C3E640>
The second one does not (result of the search is None so nothing prints):
In [4]: re.search('^two', s)
With MULTILINE ^two will match:
In [5]: re.search('^two', s, re.MULTILINE)
Out[5]: <_sre.SRE_Match object at 0x00E901E0>
Kent
More information about the Tutor
mailing list