Floyd
Floyd

Reputation: 164

How to search and replace precisely

I have a text file which content is quite like following

G12

G10,11

GO12

GO10,12

What I am trying to do is replace all the 'G' with '' but not replacing 'GO' to 'O'.

I was trying to do with regular expressions, so first I am dealing with the lines without a comma

with open(file) as f:
for line in f:
if re.match(r"%s[0-9]+" % 'G', line):
    input = line.strip('G')

And it turns out that the "G10,11\n" string is matching the regex pattern either, which makes me confused.

Is there any wrong with my statement, OR is there any simpler approach to do this? Many thanks,

Upvotes: 1

Views: 70

Answers (1)

Qtax
Qtax

Reputation: 33918

You have some alternatives here. Translating your requirements directly gets you: G(?!O), which you can replace with empty string. Which could be done like so:

line = re.sub(r"G(?!O)", "", line)

G matches a G, (?!O) matches only if the following character is not a O.

You may want to use G(?=\d) instead, to match Gs followed by a digit.

Upvotes: 4

Related Questions