Determining the unsurpassed portion of a string using regex in Python

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

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

I want to use a regular expression to search for a control word , and also use a regular expression to remove a control word from a string.

I know how to do this using something like:

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

But it falls apart if I have a few matches.

Thanks!

+4
source share
4 answers

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 
+5
source

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

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

so your example will be

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

at this point you can separate the odd elements (your coincidence) from the even elements (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 determine when, where and how many matches are found.

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

Instead of splitting or splitting, perhaps you can use re.sub and replace the empty string ("") whenever you find a pattern. For instance...

 >>> 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
source

Source: https://habr.com/ru/post/1300229/


All Articles