In python, the Backward Search confirmation mode using a regular expression is used. In python, the regular expression is used.
I learned a lot about it, including forward search, Backward Search, positive search, and negative search. Here we will learn one more, that is, the Backward Search validation mode, which means that the string has been scanned, and we want to regret it and see if it can be matched. Its syntax is :(? <= Pattern ). For example, the following example is used to identify the Twitter account. However, this pattern only matches and does not appear in the matched string, as shown below:
[Python]View plain copy
- # Python 3.6
- #
- Import re
- Twitter = re. compile (
- '''''
- # A twitter handle: @ username
- (? <= @)
- ([\ W \ d _] +) # username
- ''',
- Re. VERBOSE)
- Text = ''' This text includes des two Twitter handles.
- One for @ caimouse, and one for the author, @ caijunsheng.
- '''
- Print (text)
- For match in twitter. findall (text ):
- Print ('handle: ', match)
The output is as follows:
This text includes des two Twitter handles.
One for @ caimouse, and one for the author, @ caijunsheng.