|
String MatchingThe simplest use of the re module is to determine whether a specified substring can be found in a string. Here is an example:
This simple search is a two-step process. The first step is to call the re.search function to perform the search. In this example, re.search searches for the substring Hospital in the string XYZ Hospital and Treatment Center. The value returned by re.search depends on whether the search substring is found in the string:
The second step is to call start to determine the index of the substring in the string. Like all indexes in Python, this index starts from 0; in start, the index indicates how many characters were skipped before the substring was found. In this example, start returns 4, which indicates that the matched substring was found after four characters were skipped.
|