python regular expression screen scrub

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaf3773
    New Member
    • Jan 2012
    • 20

    python regular expression screen scrub

    Hi

    I am trying to write a python script that executes a command to screen scrub results below
    I will appreciate it very much if you can help me with a python script that can
    pick the percentage USAGE in the second column based on the supplied queue number in the first column
    Thanks in advance.

    Code:
    import re
    content = """NUMBER of Queues = 5
    SYSTEM Treshold = 80%
      ==================================================================
    
    QUEUE   USAGE   TOTAL book1   book2
    
    ------------------------------------------------------------------
    
    0001 18%   822   481    98  
    
    0002 16%   345   765    88  
    
    0003 10%   400   300   166  
    
    0004 15%   994   322    177  
    
    0005 17%   348   297    131  
    
    ---------------------------------------------------------- """
    
    m = re.match("(0001)/(\d{%})", content)           
    if m:
        print m.group(0)      
        print m.group(1)
    When i run this from the command prompt nothing shows up

    I was able to get this screen scrub done using php preg_match_all
    but i want to do the same with the re module in python.

    Please suggestions and help will be very much appreciated

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    A regex debugger like Kodos comes in handy when dealing with regular expressions.
    Code:
    >>> patt = re.compile(r"(0001) ([0-9]+)", re.MULTILINE)
    >>> m = re.search(patt, content)
    >>> m.group(1)
    '0001'
    >>> m.group(2)
    '18'
    >>>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Following are two variations showing how to get all the values.
      Code:
      for i in range(1, 6):
          patt = re.compile(r"(%04d) ([0-9]+)" % (i), re.MULTILINE)
      
          m = re.search(patt, content)
          if m:
              print m.group(1), m.group(2)
      Code:
      patt = re.compile(r"(0+[1-9]+) ([0-9]+)", re.MULTILINE)
      print patt.findall(content)

      Comment

      • kaf3773
        New Member
        • Jan 2012
        • 20

        #4
        Thank you so much bvdet. All the above you suggested do exactly what i want. I really appreciate.

        Could you kindly explain it to me

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          patt = re.compile(r"(0 *[1-9]+) ([0-9]+)", re.MULTILINE)

          A regular expression inside parentheses matches a substring as a group and the substring is saved. The substring can be accessed by the MatchObject group() method. "0*" matches 0 or more "0" characters. "[1-9]+" matches 1 or more digits. " " matches a space character. Again, "[0-9]+" matches 1 or more digits. The flag 're.MULTILINE' is actually not necessary in this case, but normally makes '^' and '$' match the beginning and end of each line instead of the beginning and end of the entire string. Using the variation "^(0*[1-9]+) ([0-9]+)", the flag 're.MULTILINE' would be required.

          Comment

          Working...