简体   繁体   中英

Python Regex to match and exclude certain content

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.

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

You can use:

>>> s = 'Sony Xperia Z2 m/Smartwatch 2'
>>> re.sub(r'\s*m/.*$', '', s)
'Sony Xperia Z2'

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'

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

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