Find more than one default re python?

0

I'm trying to rename files using regex in python, with only one default works:

def new_string(pattern):
    text = pattern.group().lower()
    renturn "{}_{}".format(text[0], text[1])

regex = re.compile(r"[a-z][A-Z]")
for x in files:
    print(regex.sub(new_string, x))

But I want to replace other patterns as well:

r"[a-zA-Z][0-9]"

How do you find the two patterns instead of one?

    
asked by anonymous 12.11.2017 / 17:08

1 answer

0

I solved the problem like this:

def replace(pattern):
    text = pattern.group().lower()
    renturn "{}_{}".format(text[0], text[1])

regex = re.compile(r"([a-z][A-Z])|([a-z][0-9])")
    for x in files:
    print(regex.sub(replace, x))

Using the | (or) operator it will test both patterns

    
13.11.2017 / 13:43