Wild Card String Comparison
Cameron Laird
claird at lairds.us
Thu Aug 28 11:58:54 EDT 2008
In article <Jgptk.19609$mh5.9473 at nlpi067.nbdc.sbc.com>,
W. eWatson <notvalid2 at sbcglobal.net> wrote:
>Is it possible to do a search for a wild card string in another string. For
>example, I'd like to find "v*.dat" in a string called bingo. v must be
>matched against only the first character in bingo, and not simply found
>somewhere in bingo, as might be the case for "*v*.dat".
.
.
.
Does this session leave any questions:
python
Python 2.4.4c0 (#2, Oct 2 2006, 00:57:46)
[GCC 4.1.2 20060928 (prerelease) (Debian 4.1.1-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = "^v.*\.dat"
>>> compiled = re.compile(pattern)
>>> compiled.match("victory.dat")
<_sre.SRE_Match object at 0xb7da2c60>
>>> ms = compiled.match("victory.dat")
>>> ms.group()
"victory.dat"
>>> compiled.match("avoid.dat")
>>> # Notice the return value of "None".
...
>>> import sys
>>> sys.exit()
?
More information about the Python-list
mailing list