Regex Expression: |

Syntax: pattern1|pattern2

Search for both of them, if either is found it is true.

Example:

 re.findall("(the|top|coder)", "Marius is one of the topcoders.")
['the', 'top', 'coder']

Regex Expression: ^

Syntax: ^pattern

This would return True only if pattern is at the starting of the given text.

Example:

re.findall("^http", "http://www.google.co.in.")
['http']

Regex Expression: $

Syntax: pattern$

Returns True if pattern is in the end of given text

Example:

 re.findall("in$", "http://www.google.co.in")
['in']

Regular Expression: .

Syntax: pattern.

Use to match a single character.

 >>> re.findall("htt.", "http://www.google.co.in")
['http']

>>> re.findall("ht..", "http://www.google.co.in")
['http']</p><p>

Regular Expression: { }

Syntax: Pattern{number of times pattern must repeat continuously}

It multiplies the pattern number of times given within the braces and then search for it in the given text. In the following example it multiplies ‘t’ twice thus pattern to be searched is ‘tt’

Example:

>>>re.findall("t{2}", "http://www.google.co.in")
['tt']

Regular Expression: { ,}

Syntax: Pattern{n,}

Matches n or more occurrences of preceding expression.

Example:

>>> re.findall("t{2}", "http://www.google.co.in"
['tt']

Basics of Regular Expressions in Python

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: