Hi,
I'm trying to write a regular expression to match a string which contains numbers.
The string can be any sentence with a number in any format, eg., 122.5, $1000, 90%.
Could anyone please help?
Thanks in advance,
Ami
4 Replies - 577 Views - Last Post: 23 September 2012 - 05:21 AM
#1
Regular expressions - multiple conditions to search for pattern
Posted 19 September 2012 - 01:16 AM
Replies To: Regular expressions - multiple conditions to search for pattern
#2
Re: Regular expressions - multiple conditions to search for pattern
Posted 19 September 2012 - 02:09 AM
This is what I tried. I have a file containing a few lines, which I have in a list.
I'm not sure whether we could use "or" operators within regular expressions. I am a newbie and I have been trying to figure this out, but could not find anything that was of help to me.
Your help is appreciated.
Thanks,
Ami
pattern = None
file = open('file.txt','r')
for line in file:
pattern = re.search('\S*'+ (('\$?(\d+)\.?(\d?)') or ('(\d+)\s?(\d+)/(\d+)') or ('(\d+)\.?(\d?)%?')), line)
if pattern:
print pattern.split("text")[-1]
I'm not sure whether we could use "or" operators within regular expressions. I am a newbie and I have been trying to figure this out, but could not find anything that was of help to me.
Your help is appreciated.
Thanks,
Ami
#3
Re: Regular expressions - multiple conditions to search for pattern
Posted 20 September 2012 - 09:53 PM
I'd be genuinely impressed with Python if it was smart enough to make that work. I think a better solution would be to pipe those regular expressions together. You can find more information on how to do that here.
This post has been edited by JackOfAllTrades: 23 September 2012 - 05:21 AM
Reason for edit:: Used bit.ly on link to fix link bug
#4
Re: Regular expressions - multiple conditions to search for pattern
Posted 23 September 2012 - 04:53 AM
could you fix the link, some tag broke it i guess.
#5
Re: Regular expressions - multiple conditions to search for pattern
Posted 23 September 2012 - 05:21 AM
Done.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|