regular expression in strings

David M. Cooke cookedm+news at physics.mcmaster.ca
Fri Oct 10 22:47:02 EDT 2003


At some point, David Bear <david.bear at asu.edu> wrote:

> I'm trying to understand how regex's are interpreted out of strings
> objects.  for example, If I have a string that has newline chars in
> it, how can I get a re.split to respect where the newlines are?
>
>>>> bs = 'in the begining, \n there were new lines, and in the end \nin lines'
>>>> re.split('^in', bs)
> ['', ' the begining, \n there were new lines, and in the end \nin lines']
>>>>
>
> I want to split the string bs.  a '^in' should have also match \nin
> where \n is the newline char.

Just amounts to reading the documentation for the re module; what you want is
>>> re.split('(?m)^in', bs)
['', ' the begining, \n there were new lines, and in the end \n', ' lines']

The (?m) construct says 'set flag re.M for this expression', where
re.M == re.MULTILINE is the flag to have ^ match the beginning of
the string and the beginning of each line (similiar for $).

It's a bit clearer with compiled patterns:
>>> pat = re.compile('^in', re.MULTILINE)
>>> pat.split(bs)
['', ' the begining, \n there were new lines, and in the end \n', ' lines']

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca




More information about the Python-list mailing list