regular expression help

G

goldtech

Hi,

say:'oooo||ooo'

The regex is eating up too much. What I want is every non-overlapping
occurrence I think.

so rtt would be:

'oooo||flfllff||ooo'

just like findall acts but in this case I want sub to act like that.

Thanks
 
T

Tim Harig

Hi,

say:
'oooo||ooo'

The regex is eating up too much. What I want is every non-overlapping
occurrence I think.

so rtt would be:

'oooo||flfllff||ooo'

Python 3.1.2 (r312:79147, Oct 9 2010, 00:16:06)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import re
m="oooocccvlvlvlvnnnflfllffccclfnnnooo"
pattern = re.compile(r'ccc[^n]*nnn')
pattern.sub("||", m) 'oooo||flfllff||ooo'
 
T

Tim Harig

Python 3.1.2 (r312:79147, Oct 9 2010, 00:16:06)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import re
m="oooocccvlvlvlvnnnflfllffccclfnnnooo"
pattern = re.compile(r'ccc[^n]*nnn')
pattern.sub("||", m) 'oooo||flfllff||ooo'
# or, assuming that the middle sequence might contain singular or
# double 'n's
pattern = re.compile(r'ccc.*?nnn')
pattern.sub("||", m) 'oooo||flfllff||ooo'
 
G

goldtech

.*? fixed it. Every occurrence of the pattern is now affected, which
is what I want.

Thank you very much.
 

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

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top