[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Regular expression issue

Ashok Prabhu

2/25/2010 7:02:00 AM

Hi,

The following is a sample of my problem. I get input from user and
store it in variable 'b'. I want to match the user input with the
contents of another variable 'a'. However I m not able to get the
exact match. Could someone help?

>>> print a
c
c+

>>> b
'c+'
>>> re.search(b,a).group()
'c'

In the above example I want re to find the string 'c+' instead of 'c'.
I want a solution without escaping the '+' symbol with backslash
because it is given by the user.

Thanks,
~Ashok.
1 Answer

Peter Otten

2/25/2010 8:11:00 AM

0

Ashok Prabhu wrote:

> The following is a sample of my problem. I get input from user and
> store it in variable 'b'. I want to match the user input with the
> contents of another variable 'a'. However I m not able to get the
> exact match. Could someone help?
>
>>>> print a
> c
> c+
>
>>>> b
> 'c+'
>>>> re.search(b,a).group()
> 'c'
>
> In the above example I want re to find the string 'c+' instead of 'c'.
> I want a solution without escaping the '+' symbol with backslash
> because it is given by the user.

>>> a = "yadda c+ yadda"
>>> b = "c+"
>>> re.search(re.escape(b), a).group()
'c+'

But if you aren't interested in anything but string literals, you should use
string search:

>>> a.index(b)
6

Peter