[Tutor] Reg Ex in python
Kent Johnson
kent37 at tds.net
Thu Jul 27 12:48:05 CEST 2006
The Fulch wrote:
> Hi,
>
> Given two strings I need to determine weather or not one of the
> strings is a subset of the other string. In at least one of the
> strings there is a star "*" which can be any character or a null there
> is a maximum of one star per string.
>
> So basically I want the function to return true if you try to match
> st*r and star
> st*r and str
> sta*rshine and star
> st* and star
> st*r and stststar
> t*r and s*ar
I'm not sure how this is a match, can you clarify?
>
>
> I tried making a function which splits the input by "*" but don't
> really know how to continue after that. I have been trying to use reg
> ex, but with little success.
If just one string has a match, you can change the * to .* and get a
regular expression to search for in the other string.
In [1]: import re
In [2]: s1='st*r'
In [3]: s2='star'
In [4]: regex = s1.replace('*', '.*')
If the search succeeds, it returns a match object:
In [5]: re.search(regex, s2)
Out[5]: <_sre.SRE_Match object at 0x00E3F6B0>
If the search fails, it returns None:
In [6]: re.search(regex, 'foo')
In [7]:
Kent
More information about the Tutor
mailing list