Python : Regular Expression

Regular expressions are text matching patterns described with a formal syntax. You’ll often hear regular expressions referred to as ‘regex’ or ‘regexp’ in conversation. Regular expressions can include a variety of rules, fro finding repetition, to text-matching, and much more.


# List of patterns to search for
pattern = ‘term1’

# Text to parse
text = ‘This is a string with term1, but it does not have the other term.’

match =, text)

# Returns a list of all matches
re.findall(‘match’,’test phrase match is in middle’)


Repetition Syntax

There are five ways to express repetition in a pattern:

1.) A pattern followed by the metacharacter * is repeated zero or more times. 
2.) Replace the * with + and the pattern must appear at least once. 
3.) Using ? means the pattern appears zero or one time. 
4.) For a specific number of occurrences, use {m} after the pattern, where m is replaced with the number of times         the pattern should repeat. 
5.) Use {m,n} where m is the minimum number of repetitions and n is the maximum. Leaving out n ({m,}) means the           value appears at least m times, with no maximum.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.