Pls, help me with re
Eric Brunel
eric.brunel at pragmadev.com
Mon Mar 3 05:20:50 EST 2003
Lexy Zhitenev wrote:
> Hi, All.
>
> I want to match a number if it is the only one in the string, and the second
> one, if '=' preceeds it.
>
> Example
>
> 'Course: 23,95' - matches '23,95'
> '$1 = 23,95 y' - matches '23,95'.
So what you want is the following:
- either a sequence of characters that are not equal signs, digits or commas,
followed by a number, followed by a group of characters that are not equal
signs, digits or commas
- or a group of any characters, followed by an equal sign, followed by a group
of characters that are not digits or commas, followed by a number, followed by
anything
Is that correct?
If it is, here is my solution:
-----------------------------------------------
import re
myREs = {
'notEqualsDigitsOrCommas' : '[^=0-9,]',
'number' : '[0-9]+(?:,[0-9]*)?',
'notDigitOrCommas' : '[^0-9]'
}
ptn1 = \
"^%(notEqualsDigitsOrCommas)s*(%(number)s)%(notEqualsDigitsOrCommas)s*$" \
% myREs
ptn2 = "^.*=%(notDigitOrCommas)s*(%(number)s)" % myREs
ptn = "%s|%s" % (ptn1, ptn2)
print re.match(ptn, 'Course: 23,95').groups()
print re.match(ptn, '$1 = 23,95 y').groups()
-----------------------------------------------
HTH
--
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
More information about the Python-list
mailing list