473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Find a number between 2 regular expressions

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
7 2163
jvdb
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
Laharl
849 Expert 512MB
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
jvdb
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
314 100+
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
jvdb
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
2,851 Expert Mod 2GB
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
ripat
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

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

Similar topics

26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
2
by: JebBushell | last post by:
I see signs that the ASP.NET regular expression validator has a different instruction set that the Find utility in VS 2003. I am trying to use the VS2003 regular expression Find tool to test...
7
by: Chris Thunell | last post by:
I'm looking to find in a long string an instance of 4 numbers in a row, and pull out those numbers. For instance: string = "0104 PBR", i'd like to get the 0104. string="PBR XT 0105 TD", i'd like...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
6
by: androoo | last post by:
Hello all, I have a string for example : strTest = "a lineof text (60) witha number in it" I need to extract the number from between the brackets, the postion of the number in brackets is...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.