Find and replace symbols with regex python
Question: 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`


Answer: 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.

