How to create a regex pattern

I want to make a refuges pattern that matches a string if it starts with one character,X, and ends with another character, y.

X.*?y

I recommend experimenting with regexes at https://regex101.com/. It also has a nice quick reference for common patterns.

2 Likes

If you matching the entire string, you might want to add anchors for the start and end of the string: \AX.*?y\Z

1 Like

+1 on regex101.com, I’ve been using it for years. One quick tip, make sure to go to the option menu on the upper left and turn on “Python” so that your regex experiments are using the proper dialect.

Unless you have to use the regex module for some reason you might want to look at some of the packages shown here mikaelho/python-human-regex: More understandable alternatives for Python regular expressions (github.com), also see Python Btes Episode #303 This title is required or is it optional? - [Python Bytes Podcast] where this was introduced.