Regexes and escape characters

Apr 13, 2010
309
7
61
The Hague
I want to SET a number of envars with a Regex that describes strings that will be passed correctly as arguments to functions and the like.

When passing envars around, to and fro subroutines, unquoting, changing and requoting again, things can go wrong. Sometimes there is quite a "distance" between the point when a mistake caused a malformed string and the moment that the error actually shows itself. By using a SET /T:regex:"..." I hope to catch those mistakes earlier.

I already tested the regex outside of TC and reads as follows in Perl notation.
Code:
^([^\s,"]+|"[\w\s,]+")$

In short it says: the whole string is acceptable if it matches an (unquoted string that contains no space, tab or commas) or (a quoted string). I know improvements are possible but I'll cross that bridge after solving this riddle.

At this point I'm stuck because I can't find the right combination of back-ticking, quoting, escaping and SETDOS expansion to get TC to swallow it.
I keep getting "Parameter error".

I have attached a test-driver and test-cases. Enjoy.

Any insights?

I pray this is not something trivial. :oops:
 

Attachments

  • TestCases.txt
    191 bytes · Views: 93
May 20, 2008
11,407
99
Syracuse, NY, USA
With the Perl syntax, this works or at least comes close.
Code:
v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$","oo,oo"]
1

v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$","oo oo"]
1

v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$",oo oo]
0

v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$",oo,oo]
0
v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$",oo"oo]
0

v:\> echo %@regex["^([^\s\^"\,]+|\".*\")$",oooo]
1

A literal double quote (always) needs RE escaping (\), and, in this case, TCC escaping (so it doesn't end @regex's first parameter). I don't know why the comma in the character class needs RE escaping (but it does).
 
Apr 13, 2010
309
7
61
The Hague
I copied your version and adapted my test script, trying to understand.
I still have some questions:
- in the left alternative you tc-esc the quotes, in the right you only rx-esc them. Why is that?
- why do we not need to double the caret at the beginning?

I ended up with
Code:
"^([^\s\^"\,]+|\"[\w\s\,]+\")$"

But the three final cases are matching and they shouldn't.
 

Attachments

  • TestCases.txt
    191 bytes · Views: 97
  • SafeStrings.BTM
    821 bytes · Views: 95
Last edited:
May 20, 2008
11,407
99
Syracuse, NY, USA
They don't match here.
Code:
v:\> echo %@regex["^([^\s^"\,]+|\"[\w\s,]+\")$", "Good, BAD and ugly"]
0

v:\> echo %@regex["^([^\s^"\,]+|\"[\w\s,]+\")$","Good","BAD and ugly"]
0

v:\> echo %@regex["^([^\s^"\,]+|\"[\w\s,]+\")$",""Good, BAD and ugly""]
0

And I think I was wrong earlier. the double quote doesn't need to be RE escaped inside [] (but it doesn't hurt and it does need to be TCC escaped).
 
May 20, 2008
11,407
99
Syracuse, NY, USA
I think all the test cases look OK.
Code:
v:\> do l in @testcases.txt ( echo %@regex["^([^\s^"\,]+|\"[\w\s,]+\")$",%l] ... %l)
1 ... Valid
0 ... So wrong
0 ... also,flaw
0 ... neater, flaw
1 ... "Ahhh"
1 ... "Some sense"
1 ... "and,sensibility"
1 ... "neater, even"
1 ... "GOOD, bad and ugly"
0 ...  "Good, BAD and ugly"
0 ... ""Good, BAD and ugly""
0 ... "Good","BAD and ugly"
 
  • Like
Reactions: djspits
Similar threads
Thread starter Title Forum Replies Date
F WAD SWITCH statement with regexes or wildcards as CASE expressions Support 9
F Regexes in REN/MOVE/… commands seem to not work at all Support 3
vefatica ECHOERR and color escape sequences Support 4
P Escape key no longer clears command line in v24 Support 21
D Can ANSI escape sequences be used in keystroke aliases? Support 8
jbanaszczyk How to escape? Support 2
tmaynard How to? Control-U alias for Escape (command line editing) Support 5
A Escape characters on right side of pipe Support 0
H Escape key no longer closes Help Support 3
M Escape-character issue... Support 7
vefatica Escape sequences and DO? Support 13
R WAD Unusable state when using Chinese characters Support 3
C TEE command appending null characters to output Support 6
vefatica Quoting file names with special characters? Support 7
vefatica How to? Add real <ESC> characters with TPIPE Support 2
C Erase all characters left or right from cursor location Support 2
J Escaped Characters and Variables Support 2
Joe Caverly Unicode, Codepage 437, and line characters Support 3
gschizas Fixed Using codepage 65001 (UTF-8) breaks non-ASCII characters Support 8
vefatica Display of special characters in aliases. Support 25
J Python: TCC command line parsing removes '=' equal sign characters Support 4
C filemasks over 172 characters fail Support 5
redwdc TCC.exe opens to 11 characters x 3 rows Support 1
G Selection characters with mouse double-click Support 3
R Function #IDOW returns only 2 characters Support 10
krischik WAD Tee printing Chinese characters Support 7
Alexander How to? how can I use Cyrillic characters in the Echo command? Support 8
jadaml Echo unicode characters from UTF-8 Batch files? Support 1
V Fixed CD ~ has trash characters in it Support 5
S International characters - TYPE vs %@LINE function Support 5
Jay Sage Getting Quote Characters into a Toolbar Tab Support 12
vefatica Fixed Escaping special characters Support 1
M Problem with Extended Search and non-ASCII characters in directories names Support 3
A How to? PhraseExpress autotext/autocompletion printing unexpected characters Support 4
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
JohnQSmith WAD DO and escaped redirect characters Support 6
W TCMD 16: double characters Support 11
MickeyF how to work with env var with special characters in the name Support 5
vefatica Fixed TPIPE doesn't /SPLIT at characters Support 0
J How to? 'tab' characters in the console and clipboard 'copy' Support 1
M How to? Determine a whether vars beginning with certain characters exist... Support 5
M Another simple question re. Take Command/TCC window witdth in characters... Support 16
U Keystack mangles 'special' characters Support 6
J dir failure with some unicode characters Support 6
vefatica Random access to the characters in a string? Support 4
nikbackm @select function and TAB characters Support 1
M Overriding meanings of some characters... Support 7
T How do you echo lines with special characters to file? Support 4
Emilio III Control characters pass through? Support 6
S Strange REN problem - non-English characters Support 3

Similar threads