简体   繁体   中英

Wanting to pull a substring from a larger string

Im wanting to pull out a "thank you!" from this string, this is my code so far

tweet = ("Twitter,Thank you!,11-15-2017 10:58:18,96,433,false,9307")
import re
twt = ""
twt = re.findall(r',(.*?),',tweet)
print(twt)

this is my output

['Thank you!', '96', 'false']

im not sure why im not only getting "Thank you!"

You're using re.findall which is giving you all the matches, you can use re.search instead:

tweet = ("Twitter,Thank you!,11-15-2017 10:58:18,96,433,false,9307")
import re
twt = ""
twt = re.search(r',(.*?),',tweet).group(1)
>>> print(twt)

>>> 'Thank you!'

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