0

I'm trying to match lines based on a regex for excluding certain characters, but not getting the correct results.

Specifically, I want to get all the lines from my file which do NOT contain any v,w,x,y or z characters.

My code is:

pat = '[^vwxyz]'
for line in records:
        if re.search(pat, line) != None:
                print(line)

but this still displays following lines:

Line 1: 32.27:meal:20170317:lunch at Clyde's with Fred and Gina, Big Inc.

Line 2: 22.00:travel:20170317:tolls

Line 3: 119.56:util:20170319:Verizon Wireless

Line 4: 284.23:util:20170323:Peoples Gas

Line 5: 8.98:supply:20170325:Flair pens

Line 6: 23.25:meal:20170223:dinner at Logan Airport

Lines 4 and 6 are expected in the result set, but others are not

1 Answers1

1

Try printing lines which do not match the following pattern:

^.*[vwxyz].*$

Code:

pat = '^.*[vwxyz].*$'
for line in records:
    # use re.I for case insensitive matching, if you want that
    if not re.search(pat, line):
        print(line)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360