0

I am new to Python.

I have a string printing a table(and its contents) just like the following

Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job     
                                                                        under   
                                                                        progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job     
                                                                        under   
                                                                        progress

I need to find how many times the string "Job under progress" is present in the table. I tried len( re.findall( pattern, string ) ) and len( re.findall("(?=%s)" % pattern, string) ) but none of them seem to work.

Any better suggestions?

2
  • Is that the exact format? Is the line Job under progress (in one line), rather than broken up in three lines as in your example? Commented Oct 28, 2013 at 6:19
  • If I am not wrong its one line. Whole row is inserted using table.newRow(..) Commented Oct 28, 2013 at 6:21

1 Answer 1

3
data = """
Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job     
                                                                        under   
                                                                        progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job     
                                                                        under   
                                                                        progress
                                                                        """
import re
print len(re.findall("Job\s+under\s+progress", data))

Output

2

Edit: If its in the same line, you dont need regEx at all

data = """
Name         At time Last  Inter\ Max    Logfile Location               Status  
                     time   val   log                                           
                           (mins) files                                         
------------ ------- ----- ------ ------ ------------------------------ ------- 
foo1           now   16:00   60   100    flash:/schedule/foo1/          Job under progress
foo2           now     -     60   100    -                              Waiting 
tech-support   now   16:00   60   100    flash:/schedule/tech-support/  Job under progress
"""

print sum(1 for line in data.split("\n") if "Job under progress" in line)

Output

2
Sign up to request clarification or add additional context in comments.

2 Comments

picky, but i would use + here.
@Eevee You are right. There will be atleast one whitespace character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.