Reputation: 6493
I'm trying to exclude some content from a string. Here is an example:
Sony Xperia Z2 m/Smartwatch 2
and:
Sony Xperia Z2 + headphones
I want to get only
Sony Xperia Z2
in both cases.
I have been able to match the string i want to get rid of with this but how do i select the inverse? What i got so far:
m/([a-zA-Z 0-9]*)
Edit: I have added another case.
Upvotes: 1
Views: 1009
Reputation: 395
Using regex split
re.split(r" m/| \+ ", yourString)[0]
This will work with both of your examples:
string1 = "Sony Xperia Z2 m/Smartwatch 2"
print re.split(" m/| \+ ", string1)[0]
# output: Sony Xperia Z2
string2 = "Sony Xperia Z2 + headphones"
print re.split(" m/| \+ ", string2)[0]
# output: Sony Xperia Z2
And if you have more separator characters, you can add them to the pattern of the split
function.
You can also use re.split(...)[1]
to retrieve the second part of your string:
string1 = "Sony Xperia Z2 m/Smartwatch 2"
print re.split(" m/| \+ ", string1)[1]
# output: Smartwatch 2
Upvotes: 2
Reputation: 2458
Something like :
test = 'Sony Xperia Z2 m/Smartwatch 2'
res = re.search('m/([a-zA-Z 0-9]*)', test)
cleanstr = test.replace(res.group(), '')
print cleanstr
And you got Sony Xperia Z2
Upvotes: 0
Reputation: 26667
Using Regex
>>> re.findall(r"[a-zA-Z0-9 ]+(?= m/)", "Sony Xperia Z2 m/Smartwatch 2")
['Sony Xperia Z2']
>>> re.findall(r"[a-zA-Z0-9 ]+(?= m/)", "Sony Xperia Z2 m/Smartwatch 2")[0]
'Sony Xperia Z2'
Using Split
>>> "Sony Xperia Z2 m/Smartwatch 2".split(" m/")[0]
'Sony Xperia Z2'
Upvotes: 1
Reputation: 784908
You can use:
>>> s = 'Sony Xperia Z2 m/Smartwatch 2'
>>> re.sub(r'\s*m/.*$', '', s)
'Sony Xperia Z2'
Upvotes: 1