Python re.findall inverse Match


 
Thread Tools Search this Thread
Top Forums Programming Python re.findall inverse Match
# 1  
Old 01-27-2016
Python re.findall inverse Match

I ask of you but yet another simplistic question that I hope can be answered. Its better explained showing my code. Here is my list(tmp_pkglist), which contains a list of all Debian (Jessie) packages:
snippet
Code:
 'zssh (1.5c.debian.1-3.2+b1 [s390x], 1.5c.debian.1-3.2 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])',
 'zsync (0.6.2-1)',
 'ztex-bmp (20120314-2)',
 'zurl (1.3.1-4)',
 'zutils (1.3-4)',
 'zutils-dbg (1.3-4) utilities for dealing with compressed files transparently (debug)',
 'zvbi (0.2.35-3) Vertical Blanking Interval (VBI)',
 'zygrib (6.2.3-1)',
 'zygrib-maps (6.2.3-1)',
 'zynadd (1+git.20100609+dfsg0-2)',
 'zynaddsubfx (2.4.3-4)',
 'zynaddsubfx-dbg (2.4.3-4)',
 'zynaddsubfx-dssi (2.4.3-4)',
 'zypper (1.11.13-1)',
 'zypper-common (1.11.13-1) command line software manager using libzypp (common files)',
 'zypper-doc (1.11.13-1) command line software manager using libzypp (documentation)',
 'zziplib-bin (0.13.62-3)',
 'zzuf (0.13.svn20100215-4.1+b1 [s390x], 0.13.svn20100215-4.1 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])']

Using this block of code and using re.findall, how can I tell the regex to "not" or inversely find all of the lines that do not contain:
Code:
 'zssh (1.5c.debian.1-3.2+b1 [s390x], 1.5c.debian.1-3.2 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])
'zzuf (0.13.svn20100215-4.1+b1 [s390x], 0.13.svn20100215-4.1 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])']

I tried various combos but cannot get the inverse of what I want which is:
Code:
 'zypper (1.11.13-1)',
 'zypper-common (1.11.13-1) command line software manager using libzypp (common files)',
 'zypper-doc (1.11.13-1) command line software manager using libzypp (documentation)',
 'zziplib-bin (0.13.62-3)',

For example:
Code:
for scrape in tmp_pkglist:
            capture = re.findall('?!(.*\[s390x\].*))', scrape)
            if capture:
                    print capture

I am used to using "!" or boolean "not" but have never done it using Pythons "re" module. Thanks in advance.

---------- Post updated at 11:49 AM ---------- Previous update was at 10:44 AM ----------

This did the trick:
Code:
for scrub_norm in tmp_pkglist:
    sanitize_norm = re.findall('.*\[s390x\].*', scrub_norm)
    if not sanitize_norm:
        print scrub_norm

Thanks to grail from antother forum
# 2  
Old 02-01-2016
From the docs at https://docs.python.org/3/library/re.html

Quote:
(?!...)Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.
Or, if you don't care what langauge is used, just pipe a grep -v like the following:

Code:
$ apt-cache search foo | grep -v bar

# 3  
Old 02-01-2016
Quote:
Originally Posted by metallica1973
...I am used to using "!" or boolean "not" but have never done it using Pythons "re" module. ...
Yes, Python uses the more verbose "not" instead of "!" for negative regex matches.

Code:
$
$ cat pkg_list.txt
'zssh (1.5c.debian.1-3.2+b1 [s390x], 1.5c.debian.1-3.2 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])',
'zsync (0.6.2-1)',
'zzuf (0.13.svn20100215-4.1+b1 [s390x], 0.13.svn20100215-4.1 [amd64, arm64, armel, armhf, i386, mips, mipsel, powerpc, ppc64el])']
'ztex-bmp (20120314-2)',
$
$ cat -n negative_match.py
     1  #!/usr/bin/python
     2  import re
     3  input_file = "pkg_list.txt"
     4  for line in open(input_file):
     5      if not re.match('.*\[s390x\].*', line):
     6          print line,
     7
$
$ python negative_match.py
'zsync (0.6.2-1)',
'ztex-bmp (20120314-2)',
$
$

For reference, here's the corresponding regex in Perl:

Code:
$
$ perl -lne 'print if !/\[s390x\]/' pkg_list.txt
'zsync (0.6.2-1)',
'ztex-bmp (20120314-2)',
$
$ perl -lne 'print if not /\[s390x\]/' pkg_list.txt
'zsync (0.6.2-1)',
'ztex-bmp (20120314-2)',
$
$

This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Shell Programming and Scripting

Python etree.ElementTree findall function

Hi everyone. I'm trying to learn python as it relates to parsing xml, and I thought I would start by trying to create an easy (or I thought would be easy) function to match on a certain attribute in an xml file and print out it's related attribute. Here's an example to hopeful make sense of... (0 Replies)
Discussion started by: timj123
0 Replies

3. Shell Programming and Scripting

Python fails to detect String Match Found

Below is my code for comparing string for Exact Match found in python. for word in jdbc_trgt.split(','): global comp comp=word.strip(); print "GloBAL:" + comp fiIn = open('list.txt').readlines() for lines in fiIn: print "line1s:" +... (6 Replies)
Discussion started by: mohtashims
6 Replies

4. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

5. Shell Programming and Scripting

Inverse Grep

Hi, I'm trying to wtite a script which actually print the text which doesn't contain a word , i mean to say. eg:- if a file contains the follwoing data Hello how ru ??? What ru doing ? what is the % of data contained ??? I want to write a script such that it prints the line excluding... (1 Reply)
Discussion started by: nagios
1 Replies

6. Shell Programming and Scripting

Inverse of Cut

Hi List, I have a CSV file which I am manipulating. Each time I receive the CSV for processing, there will be extra columns of data. Here's what I want to do; I want to be able to remove specific columns from the CSV data file, but keep the remaining columns, where the remaining columns are... (3 Replies)
Discussion started by: landossa
3 Replies

7. Shell Programming and Scripting

use python or awk to match names 'with error tolerance'

I think this is a very challenging problem I am facing and I have no idea how to deal with it Suppose I have two csv files A.csv Toyota Camry,1998,blue Honda Civic,1999,blue B.csv Toyota Inc. Camry, 2000km Honda Corp Civic,1500km I want to generate C.csv Toyota Camry,1998,blue... (7 Replies)
Discussion started by: grossgermany
7 Replies

8. Shell Programming and Scripting

matrix inverse (awk)

I need to inverse a matrix given in a file. The problem is I'm stuck with writing determinant finding algoritm into code. I found this algoritm about finding determinant of nxn matrix. This is what i need: Matrices and Determinants and here: a11 a12 a13 a21 a22 a23 a31 a32 a33... (0 Replies)
Discussion started by: vesyyr
0 Replies

9. UNIX for Dummies Questions & Answers

Inverse Video

I am trying to discover the sequence of escape characters that I need to pass ksh in order to turn inverse video on and off. I need this information for two reasons: * I want to include inverse video text in some shell scripts * I wish to highlight a section of an /etc/motd file Thanks for... (2 Replies)
Discussion started by: sam_pointer
2 Replies
Login or Register to Ask a Question