cool_jesus
cool_jesus

Reputation: 197

Find and replace symbols with regex python

I have such sample:

sample = 'TEXT/xx_271802_1A'
p = re.compile("(/[a-z]{2})")
print p.match(sample)

in position of xx may be any from [a-z] in quantity of 2:

TEXT/qq_271802_1A  TEXT/sg_271802_1A  TEXT/ut_271802_1A

How can I find this xx and f.e. replace it with 'WW':

TEXT/WW_271802_1A  TEXT/WW_271802_1A  TEXT/WW_271802_1A

my code returns None

Upvotes: 2

Views: 1502

Answers (3)

Bhargav Rao
Bhargav Rao

Reputation: 52151

You can try the following Regular expression :

>>> sample = 'TEXT/xx_271802_1A'
>>> import re
>>> re.findall(r'([a-z])\1',sample)
['x']
>>> re.sub(r'([a-z])\1','WW',sample)
'TEXT/WW_271802_1A'
>>> sample = 'TEXT/WW_271802_1A TEXT/WW_271802_1A TEXT/WW_271802_1A'
>>> re.sub(r'([a-z])\1','WW',sample)
'TEXT/WW_271802_1A TEXT/WW_271802_1A TEXT/WW_271802_1A'

The RegEx ([a-z])\1 searches for 1 letter and then matches it if it repeats immediately.

Upvotes: 2

vks
vks

Reputation: 67978

sample = 'TEXT/xx_271802_1A'
p = re.compile("(/[a-z]{2})")
print p.search(sample).group()

Your code return None as you are using match which matches from start.You need search or findall as you are finding anywhere in string and not at start.

For replacement use

re.sub(r'(?<=/)[a-z]{2}','WW',sample)

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

you only need to do this:

sample = re.sub(r'(?<=/)[a-z]{2}', 'WW', sample)

No need to check the string before with match. re.sub makes the replacement when the pattern is found.

(?<=..) is a lookbehind assertion and means preceded by, it's only a check and is not part of the match result. So / is not replaced.

In the same way, you can add a lookahead (?=_) (followed by) at the end of the pattern, if you want to check if there is the underscore.

Upvotes: 1

Related Questions