Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'negative' pattern matching in python

Tags:

python

regex

I have the following input,

OK SYS 10 LEN 20 12 43 1233a.fdads.txt,23 /data/a11134/a.txt 3232b.ddsss.txt,32 /data/d13f11/b.txt 3452d.dsasa.txt,1234 /data/c13af4/f.txt . 

And I'd like to extract all of the input except the line containing "OK SYS 10 LEN 20" and the last line which contains a single "." (dot). That is, I want to extract the following

1233a.fdads.txt,23 /data/a11134/a.txt 3232b.ddsss.txt,32 /data/d13f11/b.txt 3452d.dsasa.txt.1234 /data/c13af4/f.txt 

I tried the following,

for item in output:     matchObj = re.search("^(?!OK) | ^(?!\\.)", item)     if matchObj:         print "got item "  + item 

but it does not work, as it does not produce any output.

like image 337
JosiP Avatar asked Aug 23 '12 11:08

JosiP


People also ask

Does Python not match?

Not Equal Operator in Python If the values compared are equal, then a value of true is returned. If the values compared are not equal, then a value of false is returned. != is the symbol we use for the not equal operator.

Can you pattern match in Python?

Introducing Python structural pattern matching The match/case statement follows the same basic outline as switch/case . It takes an object, tests the object against one or more match patterns, and takes an action if it finds a match. Each case statement is followed by a pattern to match against.

What is ?! In regex?

The ?! n quantifier matches any string that is not followed by a specific string n.

How do I use the re lookup function in Python?

Python regex re.search() method looks for occurrences of the regex pattern inside the entire target string and returns the corresponding Match Object instance where the match found. The re.search() returns only the first match to the pattern from the target string.


2 Answers

See it in action:

matchObj = re.search("^(?!OK|\\.).*", item) 

Don't forget to put .* after negative look-ahead, otherwise you couldn't get any match ;-)

like image 55
mmdemirbas Avatar answered Oct 04 '22 04:10

mmdemirbas


Use a negative match. (Also note that whitespace is significant, by default, inside a regex so don't space things out. Alternatively, use re.VERBOSE.)

for item in output:     matchObj = re.search("^(OK|\\.)", item)     if not matchObj:         print "got item " + item 
like image 31
Marcelo Cantos Avatar answered Oct 04 '22 06:10

Marcelo Cantos