使用Python中的正则表达式确定字符串的不匹配部分

时间:2022-09-13 11:25:07

Suppose I have a string "a foobar" and I use "^a\s*" to match "a ".

假设我有一个字符串 “一foobar的” 我用 “^一个\ s *” 匹配 “A”。

Is there a way to easily get "foobar" returned? (What was NOT matched)

有没有办法轻松获得“foobar”返回? (什么不匹配)

I want to use a regex to look for a command word and also use the regex to remove the command word from the string.

我想使用正则表达式来查找命令字,并使用正则表达式从字符串中删除命令字。

I know how to do this using something like:

我知道如何使用以下方式执行此操作:

mystring[:regexobj.start()] + email[regexobj.end():]

But this falls apart if I have multiple matches.

但如果我有多场比赛,这会分崩离析。

Thanks!

4 个解决方案

#1


5  

Use re.sub:

import re
s = "87 foo 87 bar"
r = re.compile(r"87\s*")
s = r.sub('', s)
print s

Result:

foo bar

#2


2  

from http://docs.python.org/library/re.html#re.split

>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']

so your example would be

所以你的例子就是

>>> re.split(r'(^a\s*)', "a foobar")
['', 'a ', 'foobar']

at which point you can separate the odd items (your match) from the even items (the rest).

此时,您可以将奇数项(您的匹配项)与偶数项(其余项)分开。

>>> l = re.split(r'(^a\s*)', "a foobar")
>>> l[1::2] # matching strings
['a ']
>>> l[::2] # non-matching strings
['', 'foobar']

This has the advantage over re.sub in that you can tell, when, where, and how many matches were found.

这比re.sub更有优势,因为你可以告诉你,发现的时间,地点和数量。

#3


1  

>>> import re
>>> re.sub("87\s*", "", "87 foo 87 bar")
'foo bar'

#4


1  

Instead of splitting or separating, maybe you can use re.sub and substitute a blank, empty string ("") whenever you find the pattern. For example...

您可以使用re.sub代替分割或分离,并在找到模式时替换空白的空字符串(“”)。例如...

>>> import re
>>> re.sub("^a\s*", "","a foobar")
'foobar''
>>> re.sub("a\s*", "","a foobar a foobar")
'foobr foobr'
>>> re.sub("87\s*", "","87 foo 87 bar")
'foo bar'

#1


5  

Use re.sub:

import re
s = "87 foo 87 bar"
r = re.compile(r"87\s*")
s = r.sub('', s)
print s

Result:

foo bar

#2


2  

from http://docs.python.org/library/re.html#re.split

>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']

so your example would be

所以你的例子就是

>>> re.split(r'(^a\s*)', "a foobar")
['', 'a ', 'foobar']

at which point you can separate the odd items (your match) from the even items (the rest).

此时,您可以将奇数项(您的匹配项)与偶数项(其余项)分开。

>>> l = re.split(r'(^a\s*)', "a foobar")
>>> l[1::2] # matching strings
['a ']
>>> l[::2] # non-matching strings
['', 'foobar']

This has the advantage over re.sub in that you can tell, when, where, and how many matches were found.

这比re.sub更有优势,因为你可以告诉你,发现的时间,地点和数量。

#3


1  

>>> import re
>>> re.sub("87\s*", "", "87 foo 87 bar")
'foo bar'

#4


1  

Instead of splitting or separating, maybe you can use re.sub and substitute a blank, empty string ("") whenever you find the pattern. For example...

您可以使用re.sub代替分割或分离,并在找到模式时替换空白的空字符串(“”)。例如...

>>> import re
>>> re.sub("^a\s*", "","a foobar")
'foobar''
>>> re.sub("a\s*", "","a foobar a foobar")
'foobr foobr'
>>> re.sub("87\s*", "","87 foo 87 bar")
'foo bar'