iNTERFACEWARE Products Manual > Learning Center > Learning Python > Working With Strings > Pattern Matching in Strings > Special Characters and Pattern Matching |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
Python allows you to specify a pattern to match when searching for a substring contained in a string. This pattern is known as a regular expression. For example, if you want to find a North American-style phone number in a string, you can define a pattern consisting of three digits, followed by a dash (-), and followed by four more digits. In a regular expression, certain characters have special meanings. The table below lists these special characters and their meanings.
If you use multiple special characters in a pattern, you can use parentheses () to specify the order in which the characters are to be interpreted. Here is an example of a search that uses a regular expression:
In this example, the regular expression is (iss)+, which means "one or more occurrences of the substring iss". In the string Mississippi, this matches ississ. (It also matches iss, but the matcher tries to match as large a substring as possible.) The function group() returns the substring that was matched, and start() returns the location of the substring. If your substring contains a character that is normally interpreted as a special character, precede the character with a backslash (\):
The pattern \*+A matches one or more occurrences of the * character. In the example, this matches ***A. The backslash character is also used to define character sequences that have a special meaning in patterns. The most commonly used of these character sequences are:
For a complete list of the special character sequences that can be used in patterns, see the Python documentation for the re module. |