In the previous study of the comparison of multi-mode, there are forward search, there are back-to-search, there is a positive pattern, there is a negative mode. This time to learn one, is the post-search affirmation mode, meaning that has been scanned the string, but also want to regret to see if you can match. Its syntax is: (? <=pattern). The example below is used to identify the Twitter account, but it will only match and not appear in the matching string, as follows:
[Python]View PlainCopy
- #python 3.6
- #
- Import re
- twitter = Re.compile (
- "' "
- # A Twitter handle: @username
- (? <[email protected])
- ([\w\d_]+) # username
- ‘‘‘,
- Re. VERBOSE)
- Text = "" Thistext includes the other Twitter handles.
- One for @caimouse, and one for the author, @caijunsheng.
- ‘‘‘
- Print (text)
- For match in Twitter.findall (text):
- print (' Handle: ', match)
The resulting output is as follows:
This text includes the Twitter handles.
One for @caimouse, and one for the author, @caijunsheng.
A forward-search positive pattern using regular expressions in Python