=~ ... still don't get it

May 20, 2008
11,400
99
Syracuse, NY, USA
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?
 
Jan 19, 2011
604
14
Norman, OK
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:\]
 
May 20, 2008
11,400
99
Syracuse, NY, USA
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
12,344
149
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
570
4
Groton, CT
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).
 
Similar threads
Thread starter Title Forum Replies Date
J Batcomp still not working for me in TCMD27 Support 1
B Home and Work installations still allowed with one license? Support 2
D [cosmetic] Shortcut for v24 still has v23 in its comment Support 1
C v23 Everything still referenced after removing v23 and installing v24 Support 7
vefatica WAD ANSI still misbehaves with build 35 Support 36
V Still installing to TC21 Support 0
Alpengreis Installer: text is still truncated in german language Support 2
vefatica WAD DIR /HL still gets names wrong Support 16
vefatica "View" settings still not remembered Support 10
vefatica Documentation Help: XML link still broken Support 0
D Documentation Still waiting for the promissed lists Support 5
vefatica WAD IFTP - old problem still there Support 2
WavSlave Fixed Escaping out of SELECT sometimes still executes the command Support 2
T 'All Downloads' page still links to v13 Support 2
J Still problems understanding "foldermonitor" Support 9
H UNZIP /F and /U still not right 12.0.27 Support 0
A Bug report: Control-C still not working Support 9
R Bug TPIPE's pdf to text conversions don't work Support 2
vefatica Console popup windows don't work correctly Support 0
M Backquoted parameters used in GoSub don't pass string as a single parameter Support 2
V Ctrl-Home/End don't work in Win10 Support 3
vefatica Documentation Help: links to "Conditional expressions" don't work Support 2
RChrismon How to? Changes to Options Don't Save Support 8
vefatica Unicode ... I don't understand Support 1
MickeyF WAD 'if' and aliases don't mix so well Support 5
D How to? V15.00.30 -(help!) - Floating view windows don't open Support 1
vefatica Backquotes, aliases get 'em, BTMs don't? Support 1
D Automatic directory changes don't work at all Support 4
W redirection with pipe don't work Support 10
vefatica @PID, ISAPP - don't see system processes Support 12
J Fixed IDE search keys don't work in TCMD 13.01.32 Support 3
M An issue I really don't understand and is too long and detailed for this "Title" line... Support 16
vefatica "Administrator:" - I don't want to see it Support 0
C NTFS Descriptions don't work? Support 3
B What Don't I Understand About @SERVICE Support 9
R Don't wake up external drives on startup Support 2

Similar threads