简体   繁体   中英

Extract everything before a particular string in python

Let's say I have a string

s = 'ab@cD!.2e.cp'

I want to extract only ab@cD!.2e out of it. I am trying this:

print(re.search(r'^(.*?)\.cp',s).group())

But still getting the output as ab@cD!.2e.cp . Can someone please tell me where I am doing it wrong and what should be the correct regex for this?

You probably meant to add 1 as parameter to group:

import re
s = 'ab@cD!.2e.cp'
re.search(r'^(.*?)\.cp',s).group()      # 'ab@cD!.2e.cp'
re.search(r'^(.*?)\.cp',s).group(0)     # 'ab@cD!.2e.cp'
re.search(r'^(.*?)\.cp',s).group(1)     # 'ab@cD!.2e'

Instead of re.search , use re.findall :

import re
s = 'ab@cD!.2e.cp'
print(re.findall(r'^(.*?)\.cp',s)[0])

Output:

ab@cD!.2e

If it is really just about extracting everything before a certain string - as your title suggests - you don't need a regex at all but a simple split will do:

res = s.split('.cp')[0]

yields

'ab@cD!.2e'

Please be aware that this will return the original string if .cp was not found:

s = 'foo'
s.split('.cp')[0]

will return

'foo'

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