Find more than one standard re python?

Asked

Viewed 167 times

0

I’m trying to rename files using regex in python, with only one standard 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 also want to replace other standards:

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

How to find the two patterns instead of one?

  • I found the solution using |

  • 1

    You can answer your own question by describing the solution in detail, to help others who have the same problem in the future.

  • 1

    yes, the correct answer is using the "|" regular expression operator. Now you have the option to write an answer to your own question (including an example, etc...), so that your question can help others in the future.

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 operator | (or) it will test the two patterns

Browser other questions tagged

You are not signed in. Login or sign up in order to post.