Python Regular Expressions
Table of Contents


In Python, all functionality related to regular expressions is contained in the re module.

$ end of the string
^ start of the string
\b a word boundary must occur right here
M? Matches an optionally M character

'\' character in a string must itself be escaped. To work around that you can use a raw string, by prefixing the string with the letter r.

Example: match 'ROAD' when it's a whole word by itself anywhere in the string.

re.sub(r'\bROAD\b', 'RD.', s)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.