简体   繁体   中英

Return the content of a Wildcard match in Python

Is it possible to return the contents that match a wildcard (like .*) in a regex pattern in Python?

For example, a match like:

re.search('stack.*flow','stackoverflow') 

would return the string 'over'.

Use a capturing group:

>>> import re
>>> re.search('stack(.*)flow', 'stackoverflow').group(1)
'over'

Yes, you can capture your result. For this, just use the ()

matchobj = re.search('stack(.*)flow','stackoverflow') 
print(matchobj.group(1)) # => over

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM