简体   繁体   中英

How to search for a specific pattern in a string using regrex

I want to search for a specific pattern name in the below string using regrex. The name occur after the selfie_

for example:

name = OKAH_AGHOLOR

url = 'https://ignite-api.s3.us-east-2.amazonaws.com/live/selfie_OKAH_AGHOLOR_22342028078-IND-1619350764067.jpeg'

I have tried the below regrex expression

pattern = re.compile(r'_[A-Za-z_]+')
sequence = url
pattern.search(sequence).group()

output:

'_OKAH_AGHOLOR_'

Use lookarounds so the _ at the beginning and end are not included in the match.

pattern = re.compile(r'(?<=_)[a-z_]+(?=_)', re.IGNORECASE)

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