Problems with regexps
Ben Finney
bignose-hates-spam at and-benfinney-does-too.id.au
Fri Nov 7 18:01:35 EST 2003
On Fri, 07 Nov 2003 23:00:34 GMT, Kirk Strauser wrote:
> sample = 'FOO= BAR'
>
> if re.search(r'[^=]\s+BAR', sample):
> print 'Match 1.'
>
> if re.search(r'[^=]\s*BAR', sample):
> print 'Match 2.'
>
> When run, it produces "Match 2.". Why? I want to match:
>
> 1) Anything but '=', followed by
> 2) Zero or more spaces, followed by
> 3) 'BAR'
To understand the answer, you must understand the question.
If you inspect the MatchObject returned from re.search(), you'll learn
more about what's going on:
>>> import re
>>> sample = 'FOO= BAR'
>>> match = re.search( r'[^=]\s+BAR', sample )
>>> match
The first match returns the None object.
>>> match = re.search( r'[^=]\s*BAR', sample )
>>> match
<_sre.SRE_Match object at 0x4021b090>
The second match returns a Match object; let's see what it matched.
>>> match.group(0)
' BAR'
>>>
The matching string contains the space ("Anything but '='"), followed by
nothing ("Zero or more spaces"), followed by BAR. Exactly what you
asked for.
--
\ "It is seldom that liberty of any kind is lost all at once." |
`\ -- David Hume |
_o__) |
Ben Finney <http://bignose.squidly.org/>
More information about the Python-list
mailing list