Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

When to use re.M and re.X? (re.M versus re.X)

When to use re.M and re.X? (re.M versus re.X)

2 Answers

re.M (or re.multiline) relates to how a string is searched. It changes the behaviour of the '^' and '$' characters, allowing you to match against the start and end of lines rather just the start and end of the string. Useful for multiline strings.

re.X (or re.verbose) relates to how you write your regular expression. It lets you spread it over multiple lines to make it more readable.

The full documentation for both can be found here, and is definitely worth a read. There's no way you're going to remember everything relating to regular expressions (at least I can't), and this is one of the areas of the Python documentation I've found myself looking at most frequently:

https://docs.python.org/3.6/library/re.html

Thanks Stuart Wright !!!