background preloader

Google Python Class Day 1 Part 2

Google Python Class Day 1 Part 2

Python Regular Expressions - Educational Materials Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python. The Python "re" module provides regular expression support. In Python a regular expression search is typically written as: match = re.search(pat, str) The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. str = 'an example word:cat!!' The code match = re.search(pat, str) stores the search result in a variable named "match". The 'r' at the start of the pattern string designates a python "raw" string which passes through backslashes without change which is very handy for regular expressions (Java needs this feature badly!). Basic Patterns The power of regular expressions is that they can specify patterns, not just fixed characters. a, X, 9, < -- ordinary characters just match themselves exactly. findall

Related: