Wild Card String Comparison

W. eWatson notvalid2 at sbcglobal.net
Thu Aug 28 18:08:04 EDT 2008


Cameron Laird wrote:
> 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()
> 
> ?
Looks good. re = regular expressions.

-- 
            Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

              (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
               Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

                     Web Page: <www.speckledwithstars.net/>



More information about the Python-list mailing list