2

Lots of great regex answers here but I haven't been able to find anything that works for me. Here is the situation:

I have a large list of numbers. Let's say it is just a list of numbers from 1 to 100 and I only want to use the list that contains 10, 20, and 50. But the way the code is built (and can't be changed!) is to ignore by the regex expression not accept. So, I can't just say ^10$|^20$|^50$ instead I need to NOT them and then AND them.

I have tried this:

(?!^10$)(?!^20$)(?!^50$) 

with no luck and can't seem to find anything anywhere that is working.

Any thoughts? Many Thanks!

p.s. I just made this particular example up to show what I am doing, I wouldn't be using regex is this was the actual problem I had....:-)

4

1 回答 1

1

如果我正确理解您的问题,您需要的是这样的:

(?!^10$)(?!^20$)(?!^50$)^.*$

当该行有不同于"10","20"或的内容时,这匹配"50"

但是,这似乎是处理您的问题的一种相当麻烦的方式。这条线上的更多内容不是更好吗?

import re
pattern = re.compile(r"^(?:10|20|50)$")
for text in list_of_texts:
  m = pattern.match(text)
  if m is not None:
    print "Found something interesting (not 10, nor 20 nor 50): %s" % text
于 2012-05-15T23:25:16.557 回答