正規表現の一致に基づいて列名を置き換えるスクリプトを作成しています。 すべての列を反復処理し、正規表現を新しい文字列と一致させます。 番号で始まる文字列に置き換える場合を除いて、列名を短縮するのにうまく機能し、便利です。これは、後方参照が混乱するためです(以下の場合、\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つあります。パラメータ2はパターンで、多くの人が期待する通りエスケープを使用しますが、\Q \U \L \E \u \l はここには存在しません。パラメータ3は置換で、\Q \U \L \E \u \l は使用できます。後方参照 \1 \2 \3 ... は、パラメータ2とパラメータ3の両方で使用できます。
編集 2025年8月14日: 実際には、\U\u\L\l はパラメータ2で使用でき、1文字に一致します。u は大文字、U は大文字以外、l は小文字、L は小文字以外です。また、\A と \Z は開始位置と終了位置に一致します。