Re: partial / wildcard string match in 'in' and 'list.index()'

From: Josiah Carlson (jcarlson_at_uci.edu)
Date: 06/04/04


Date: Fri, 04 Jun 2004 00:25:07 -0700


> These would work, but I was wondering if there was some compact
> way to get 'in' and lists.index() to do wildcard matching as opposed
> to exact matching.

Short answer: no.

Long answer:
There is a regular expression library, aptly called 're', for non-exact
string searching.

The standard string methods only do exact searching likely because there
are algorithms that make it fast (linear in the length of the searched
string), that are also quite small.

There exist very simple looking regular expressions that force searches
to take exponential (in the length of the pattern) time to search, and
are generally fairly large.

An example of an exponentially slow regular expression is contained in
the following example...
>>> import re
>>> import time
>>> def test(n):
... a = 100*'0'
... t = time.time()
... re.search('(0+)'*n, a)
... return time.time()-t
...
>>> for i in xrange(18, 23):
... print i, test(i)
...
18 0.141000032425
19 0.266000032425
20 0.530999898911
21 1.07899999619
22 2.1400001049
>>>

Trust me, you don't want that kind of slow down when you are searching
in strings with "if i in 'somestring':".

  - Josiah



Relevant Pages

  • Re: how to split a string using ,fixed character length, variable text delimmiter
    ... a string in encountered that is at least 20 characters long, ... you could try using a regular expression such as this: ... you'd just be searching the text in question for occurrences of the ... out whether any given string has the correct maximum length. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about Sun JAVAC
    ... >> searching a directory and all subdirectories. ... Finding a string within a file is linear in the length of the file unless ... using an "open" version of grep. ... Regular expression were designed so that you do not need messy stuff ...
    (comp.programming)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get regular expression
    ... regular expression solution, which was about as close as one could get to ... first string. ... explode "ABLATION" and see subnodes of "ENDOMETRIAL ... "Heart 27.33/2" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)