regular expression in strings

D

David Bear

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.

Ultimately I have a large string buffer that I want to split at each
occurence of a particular regular expression, where the result
contains everything from the occurence of the regex till the end of
the 'line'

any pointers?

David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"
 
D

David M. Cooke

At some point said:
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['', ' 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:['', ' the begining, \n there were new lines, and in the end \n', ' lines']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top