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

Find a number between 2 regular expressions

P: 4
Hello,

I'm pretty new to Python.

Can someone please show me how to find a number between 2 regular expressions?
Something like this:

Text:
' MyText (mymessage): 0 (Time 0 hours)'
' MyText (mymessage): 344 (Time 34 hours)'
' MyText (mymessage): 1000 (Time 2000 hours)'

I'd like to "grep" for:
0
344
1000

And then later grep for:
0
34
2000

But am sure I will manage the second one when I can figure out how to do the first one.

I've played with the "re" function, but the output looks like this:
[' 0 ']

And I can't figure out how to get rid of the white space and quotes. The plan is to get the integer and compare that with another integer.

Thank you
Jul 25 '08 #1
Share this Question
Share on Google+
7 Replies

P: 4
That output from my "re" did not post right, there are white spaces before the first quote and the 0.
Jul 25 '08 #2

Expert 100+
P: 849
You can use the string function strip() to remove all whitespace from it (or lstrip()/rstrip() for just one end). As to the regex, what's your actual regex pattern?
Jul 25 '08 #3

P: 4
Hello,

Thanks for the reply.

This is my regexp:

s=' MyText (mymessage): 1000 (Time 2000 hours)'
pattern = re.compile(r':(.*?)\(')

pattern.findall(s)

I'm still playing with the pattern to try and figure out how the whole thing works.

Cheers
Jul 25 '08 #4

jlm699
100+
P: 314
pattern = re.compile(r':(.*?)\(')
Seeing that there is a space in your text on either side of the number, you should add spaces to your pattern so that it is like this:
Expand|Select|Wrap|Line Numbers
  1. pattern = re.compile(r': (.*?) \(')
  2.  
Jul 25 '08 #5

P: 4
Thanks for that.

There won't always be only 1 space on either side though.

My output looks something like this:

>>> s=' MyText (mymessage): 100 (Time 2000 hours)'
>>> pattern = re.compile(r':(.*?)\(')
>>> pattern.findall(s)
[' 100 ']


How do I get rid of the annoying [' and '] ?

I want to be able to say, x=100, compare that to y.
Jul 25 '08 #6

bvdet
Expert Mod 2.5K+
P: 2,851
I would probably do something like this:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s1 = 'MyText (mymessage):   0   (Time 0 hours)'
  4. s2 = 'MyText (mymessage): 344(Time 34 hours)'
  5. s3 = 'MyText (mymessage):1000 (Time 2000 hours)'
  6.  
  7. patt = re.compile(r'\): *([0-9]+) *\(')
  8.  
  9.  
  10. for s in [s1,s2,s3]:
  11.     m = patt.search(s)
  12.     if m:
  13.         print int(m.group(1))
The pattern in parentheses (one or more digits, matching as many as possible) is saved as matchObj.group(1). You can have no spaces or multiple spaces surrounding the digits.
Jul 25 '08 #7

P: 1
Hi,

This is my first post on this forum and my very first attempt in Python. So forgive me if my answer is not to the point.

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s="""
  4. MyText (mymessage):   0   (Time 0 hours)
  5. MyText (mymessage): 344(Time 34 hours)
  6. MyText (mymessage):1000 (Time 2000 hours)
  7. """
  8.  
  9. patt = re.compile(r':[^0-9]*([0-9]+)[^0-9]+([0-9]+)')
  10.  
  11. print patt.findall(s)
This returns:
Expand|Select|Wrap|Line Numbers
  1. [('0', '0'), ('344', '34'), ('1000', '2000')]
Jul 27 '08 #8

Post your reply

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