Re: regexp to match any initial substring of a string?
- From: Aric Bills <aric.bills@xxxxxxxxx>
- Date: Fri, 16 Sep 2011 13:57:26 -0700 (PDT)
On Sep 16, 11:00 am, Alexander Rast <alex.rast.techni...@xxxxxxxxx>
wrote:
This is either going to be easy, or impossible; I can't determine
which.
I want to search for words that form any legitimate leading substring
of a long potential string.
So let's say I had the string supercalifragilisticexpialidocious as a
match target, I want to match any word in the set {s su sup supe ...
supercalifragilisticexpialidociou supercalifragilisticexpialidocious}
occurring in the search string. (The search should use greedy matching
so as to suck up as much of the target as it can find)
It doesn't take much thought to realise how tedious this could become
using regexp if you have to define a separate branch for each
possibility. The problem is especially onerous if (such as I have) you
have several such long strings each of which should be tested
against.
Is there a regexp for this?
No need for regular expressions. Keep it simple:
set target supercalifragilisticexpialidocious
set test supe
if {[string range $target 0 [string length $test]-1] eq $test} {
puts "$test is a prefix of $word"
}
.
- References:
- regexp to match any initial substring of a string?
- From: Alexander Rast
- regexp to match any initial substring of a string?
- Prev by Date: Re: Build Tcl and Tk on Solaris 64 bit?
- Next by Date: tk text widget
- Previous by thread: Re: regexp to match any initial substring of a string?
- Next by thread: Re: regexp to match any initial substring of a string?
- Index(es):
Relevant Pages
|