我正在編寫一些腳本來根據正則表達式匹配替換列名稱。 我迭代所有列並將正則表達式與新字串進行匹配。 對於縮短列名稱來說效果很好且方便 - 除非我替換為以數字開頭的字串 - 這是因為反向引用變得混亂(在下面的情況下 \1 變成 \133 )
在 python 中,有「明確的反向引用」來解決這個問題。 JSL裡有同樣的嗎? 我在某處讀到它可能使用 Perl 類型反向引用,但找不到記錄的位置。
謝謝,
桃
colName = "COLUMNNAMETEST LONGNAMESHORTEN33339E0HS KEEPTHIS";
Rename_regx = "LONGNAMESHORTEN2339E0HS111";
Rename_to = "39E0H";
newname=Regex( colName,
"(.*?)(" || Rename_regx || ")(.*)", // match
"\1" || Rename_to || "\3") // replace match with
// "\g<1>" || Rename_to || "\g<3>") // python like unambiguous doesn't owrk
colName = "COLUMNNAMETEST LONG\1NAMESHORTEN2339E0HS111 KEEPTHIS";
Rename_regx = substitute("LONG\1NAMESHORTEN2339E0HS111","\","\\");
Rename_to = "39E \1 0H";
// \Q starts a sequence that ignores escapes
newname=Regex( colName,
"(.*?)(" || Rename_regx || ")(.*)", // match
"\1\Q" || Rename_to || "\E\3"); // replace match with
show(newname); // newname = "COLUMNNAMETEST 39E \1 0H KEEPTHIS";
// notice:
// \Q begins "quoting" and \E ends it
// the \Q has a side effect of ending the \1 (which is what you asked)
// the \1 in the replacement is NOT expanded inside the \Q...\E
// at the very top, it takes care of escaping any escapes with substitute,
// which *could* be an issue
// what else? upper and lower case:
// \L begin lower casing
// \U begin upper casing
// \E ends *all* \L, \U, or \Q
show(regex("aBcDEf","(..)(..)(..)","\1\L \2 \U\3"));//"aB cd EF"
// \l (lower case L) lower case the next character
// \u upper case the next character
show(regex("ABcdEF","(..)(..)(..)","\1 \u\2 \l\3"));//"AB Cd eF"
// the Q, L, and U modes share a common E (end)
// the q and l are not persistent and only apply to the next character.
// upper/lower is probably very ASCII specific, no Unicode magic.
// this is poorly documented here: https://www.jmp.com/support/help/en/17.2/index.shtml#page/jmp/escaped-characters-in-regular-expressions.shtml
// somehow all the escapes have been lumped together in the worst possible way.
編輯:regex() 函數有兩個參數使用了轉義。參數 2 是模式,它按照大多數人的預期使用轉義,而 \Q \U \L \E \u \l不屬於那裡。參數 3 是替換,而 \Q \U \L \E \u \l可以在那裡使用。反向引用 \1 \2 \3 ... 可以在 parm2 和 parm3 中使用。
編輯於 2025 年 8 月 14 日:實際上,\U\u\L\l 可以在參數 2 中使用,並且匹配單個字符:u 為大寫字符,U 為非大寫字符,l 為小寫字符,L 為非小寫字符。 \A 和 \Z 也可以符合起始位置和終止位置。