Python Regular Expression

Definition : A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.

Image Source: w3schools

Example:

Image Source: Automate the Boring Stuff with Python by Al Sweigart

Steps for Regular Expression Matching:

  1. Import the regex module with import re.

2. Create a Regex object with the re.compile() function. (Remember to use a raw string.)

3. Pass the string you want to search into the Regex object’s search() method. This returns a Match object.

4. Call the Match object’s group() method to return a string of the actual matched text.

Note: You can find all matching occurrences with the findall() method.

More Characters:

Summary:

Matching Multiple Groups with the Pipe

Optional Matching with the Question Mark

Matching Zero or More with the Star

Matching One or More with the Plus

Matching Specific Repetitions with Curly Brackets

Project Example: Find dates appear in a text

Functions:

Normal: match format month/day/year

normal_month_date: match format month/day

normal_year: match year

holidays: match holidays

weeks = match weekdays

months = match months (e.g. January, Jan., JAN)

weeks_time: match the format weeks + time (e.g. Monday 8a.m.)

weeks_periods: match the format weeks + periods (e.g. Monday afternoon)

months_date: match exact date (e.g. January 5th, 2015)

months_date2: match date without year (e.g. January 5th)

weeks_date: match the format of weeks + Month (e.g. Monday, January 15th)

months_date_with_para1: e.g. match the format of the 25th of February

months_date_with_para2: e.g. match the format of the third of Oct.