我正在编写一些脚本来根据正则表达式匹配替换列名称。 我迭代所有列并将正则表达式与新字符串匹配。 对于缩短列名称来说效果很好且方便 - 除非我替换为以数字开头的字符串 - 这是因为反向引用变得混乱(在下面的情况下 \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 还可以匹配起始位置和终止位置。