2022-03-17

How to match a string start and end with a character, don't care about the length of the string (using regex python)? [duplicate]

pattern = r"^gr.y$"
if re.match(pattern, "grey"):
    print('Match1') #Match1
if re.match(pattern, "greeey"):
    print('Match2') #Don't match
if re.match(pattern, "greeeeeeeeey"):
    print('Match3') #Don't match

Above is my code, but I want every string that starts with "gr" and ends with "y" satisfy the condition. I wonder if there and or condition in regex? Maybe satisfy condition 1 and condition 2, condition 1 or condition 2, startwith 1 and endwith 2?



No comments:

Post a Comment