=~ ... still don't get it

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
I'm having a tough time understanding how the =~ test works.

Code:
v:\> if de =~ e+$  echo yes
yes

v:\> if "de" =~ "e+$"  echo yes

v:\> if "a b c" =~ ". . ." echo yes
yes

v:\> if "a b c" =~ ". ." echo yes

v:\>
Above, the first is expected (?). Why not the same result in the second? [In my experience, both strings should be un-quoted before handing them off to Oniguruma]. The fact that the first works suggests that any match found yields a positive result. Then why is the fourth one above negative?
 
#2
Then why is the fourth one above negative?
My guess is that it is using the double-quotes as part of the regex and
Code:
". ." ( dquote anychar space anychar dquote )
is not found.

Using variables to hold the values works
Code:
[J:\]set test1=a b c

[J:\]set test2=. .

[J:\]if %test1 =~ %test2 echo yes
yes
Try using grouping (parentheses) instead of double-quotes
Code:
[J:\]if "a b c" =~ (. .) echo yes
yes

[J:\]if "a b c" =~ (?:. .) echo yes
yes

[J:\]if "a b c" =~ (?:.  .) echo yes

[J:\]
 
#3
On Tue, 14 Jun 2011 12:50:03 -0400, JohnQSmith <> wrote:

|My guess is that it is using the double-quotes as part of the regex

The regex may **need** to be quoted *for TCC's sake) because of spaces or
special characters; the same for the string.

Suoopse you want to test whether a filename (maybe with spaces) has the form
.*\d\d\.txt (any text, two digits, .txt). If the quotes are left on the string
(filename) then the regex must look for them ... cumbersome because it must be
escaped fot TCC and for the regex.

Code:
v:\> if "abc 55.txt" =~ \d\d\.txt\^"$ echo yes
yes

v:\> if "abc 55.txts" =~ \d\d\.txt\^"$ echo yes

v:\>
And as noted, that fails if the regex is quoted.

Code:
v:\> if "abc 55.txt" =~ "\d\d\.txt\^"$" echo yes

v:\>
 

rconn

Administrator
Staff member
May 14, 2008
10,097
85
#4
Above, the first is expected (?). Why not the same result in the second? [In my experience, both strings should be un-quoted before handing them off to Oniguruma]. The fact that the first works suggests that any match found yields a positive result. Then why is the fourth one above negative?
IF / IFF do not remove double quotes. They are not delimiters, they are part of the arguments. (See CMD.EXE.)
 
May 29, 2008
516
3
Groton, CT
#5
and the help file needs help, too

Also, a help non-nit (at least from my perspective) is that the new !~ and =~ should have index entries in the help (like != and == do).