简体   繁体   中英

Python re.sub always matches?

I have an array that looks something like:

mylist = [
  "blah blah hello",
  "\nbarnacles and stuff()",
  "\nhello again",
  "\nother stuff )"
]

And my regex is like the following:

s = 'hello'
rx = re.compile(s + '.*')
newlist = [ rx.sub(thing, '') for thing in mylist ]

I was expecting newlist to be:

[
  "blah blah",
  "\nbarnacles and stuff()",
  "\n",
  "\nother stuff )"
]

Instead, I got:

[
  "",
  "",
  "",
  ""
]

What's going on? This doesn't behave the same way in the REPL...

Look closely at the signature for the sub method of compiled regexes :

sub(repl, string, count=0)

The first argument is the replacement string, and the second argument is the string to operate on, which is the opposite of how you're trying to call it. (Note that this is the same relative argument order as for the re.sub function.)

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