By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,642 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,642 IT Pros & Developers. It's quick & easy.

python regular expression screen scrub

P: 20
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.

Expand|Select|Wrap|Line Numbers
  1. import re
  2. content = """NUMBER of Queues = 5
  3. SYSTEM Treshold = 80%
  4.   ==================================================================
  5.  
  6. QUEUE   USAGE   TOTAL book1   book2
  7.  
  8. ------------------------------------------------------------------
  9.  
  10. 0001 18%   822   481    98  
  11.  
  12. 0002 16%   345   765    88  
  13.  
  14. 0003 10%   400   300   166  
  15.  
  16. 0004 15%   994   322    177  
  17.  
  18. 0005 17%   348   297    131  
  19.  
  20. ---------------------------------------------------------- """
  21.  
  22. m = re.match("(0001)/(\d{%})", content)           
  23. if m:
  24.     print m.group(0)      
  25.     print m.group(1)
  26.  
  27.  
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
Feb 1 '13 #1

✓ answered by bvdet

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

Share this Question
Share on Google+
4 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
A regex debugger like Kodos comes in handy when dealing with regular expressions.
Expand|Select|Wrap|Line Numbers
  1. >>> patt = re.compile(r"(0001) ([0-9]+)", re.MULTILINE)
  2. >>> m = re.search(patt, content)
  3. >>> m.group(1)
  4. '0001'
  5. >>> m.group(2)
  6. '18'
  7. >>> 
Feb 1 '13 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Following are two variations showing how to get all the values.
Expand|Select|Wrap|Line Numbers
  1. for i in range(1, 6):
  2.     patt = re.compile(r"(%04d) ([0-9]+)" % (i), re.MULTILINE)
  3.  
  4.     m = re.search(patt, content)
  5.     if m:
  6.         print m.group(1), m.group(2)
Expand|Select|Wrap|Line Numbers
  1. patt = re.compile(r"(0+[1-9]+) ([0-9]+)", re.MULTILINE)
  2. print patt.findall(content)
Feb 1 '13 #3

P: 20
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
Feb 1 '13 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Feb 2 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.