1.re'?'
Non-greedy way, only match 0 or 1 fragment defined by the previous regular expression
zqgsb = "are you_ok_today_you_ok_today"print(re.search(r"you(.*?)today", zqgsb).groups())print(re.search(r"you(.*)today", zqgsb).groups()span>)
If you don't add ?, it will match as many strings as possible, if you add ?, it will only match 0 or 1
The output is:
('_ok_',)('_ok_today_you_ok_',)
2.()
Create a "capture" group, group regular expressions and remember matched text
print(re.search(r"you(.*?)today(.*)", zqgsb).groups())
The output is the content matched by the two parentheses captured:
('_ok_', '_you_ok_today')
3.?:
Parentheses create a "capture" group in the regular expression, turn off capture by adding "?:" at the beginning
print(re.search(r"you(?:.*?)today(.*)", zqgsb).groups())print(re.search(r"you(.*?)today(?:.*)", zqgsb).groups())
The output is:
('_you_ok_today',)('_ok_',)