Re: regexp to match any initial substring of a string?



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"
}
.



Relevant Pages

  • Re: regexp to match any initial substring of a string?
    ... of a long potential string. ... supercalifragilisticexpialidociou supercalifragilisticexpialidocious} ... set re ^[lindex $letters 0] ...
    (comp.lang.tcl)
  • Re: regexp to match any initial substring of a string?
    ... of a long potential string. ... match target, I want to match any word in the set {s su sup supe ... ... supercalifragilisticexpialidociou supercalifragilisticexpialidocious} ... Is there a regexp for this? ...
    (comp.lang.tcl)
  • regexp to match any initial substring of a string?
    ... of a long potential string. ... match target, I want to match any word in the set {s su sup supe ... ... supercalifragilisticexpialidociou supercalifragilisticexpialidocious} ... Is there a regexp for this? ...
    (comp.lang.tcl)
  • Re: regexp to match any initial substring of a string?
    ... of a long potential string. ... match target, I want to match any word in the set {s su sup supe ... ... supercalifragilisticexpialidociou supercalifragilisticexpialidocious} ... Is there a regexp for this? ...
    (comp.lang.tcl)
  • Re: unboxing problem
    ... all the correct type information and you wouldn't need to cast ... with a string or textboxes and labels for this. ... the code you posted will have to check the type of the target data structure every time you want to write something. ... This could be fixed by making the parameter passed by reference, but it's fairly awkward to do that with a variable length parameter list (essentially what you wind up doing is not taking advantage of the variable length semantics at all, instead passing an actual array you can get at later, modifying the element of interest in the array inside the method, and then in the code that called the method, copying any new values back out of the array after calling the method). ...
    (microsoft.public.dotnet.languages.csharp)