Introduction to regular expressions
Regular expressions are a language used to match specific occurrences of strings by following their pattern across the whole string. When a match is found, the resulting matched string will be returned back to user and will be held inside a structure in Python format, such as tuple
, list
, or dictionary
. The following table summarizes the most common patterns in regular expressions:

Also, one of the important rules in regular expressions is you can write your own regex and surround it with parentheses ()
, which is called the capturing group and helps you to hold important data to reference it later using the capturing group number:
line = '30 acd3.b2c6.aac9 FastEthernet0/1' match = re.search('(\d+) +([0-9a-f.]+) +(\S+)', line) print match.group(1) print match.group(2)
Note
PyCharm will automatically color strings written as regular expressions and can help you to check the validity of a regex before applying it to data. Make sure the Check RegExp
intention...