Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - Replace all occurrences that match regular expression

I have a regular expression that searches for a string that contains '.00.' or '.11.' as follows:

.*.(00|11)..*

What I would like to do is replace all occurrences that match the pattern with 'X00X' or 'X11X'. For example, the string '.00..0..11.' would result in 'X00X.0.X11X'.

I was looking into the Python re.sub method and am unsure of how to do this effectively. The returned match object only matches on the first occurrence and therefore doesn't work well. Any advice? Should I just be using a string replace for this task? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

re.sub() (docs for Python 2 and Python 3) does replace all matches it finds, but your use of .* may have caused the regex to match too much (even other occurences of .00. etc.). Simply do:

In [2]: re.sub(r".(00|11).", r"X1X", ".00..0..11.")
Out[2]: 'X00X.0.X11X'

Note that patterns cannot overlap:

In [3]: re.sub(r".(00|11).", r"X1X", ".00.11.")
Out[3]: 'X00X11.'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...