Push
Push

Reputation: 153

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?

Upvotes: 0

Views: 1259

Answers (3)

Cleb
Cleb

Reputation: 25997

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'

Upvotes: 1

nicolas
nicolas

Reputation: 3280

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'

Upvotes: 3

Ajax1234
Ajax1234

Reputation: 71451

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

Upvotes: 2

Related Questions