정규식 일치를 기반으로 열 이름을 바꾸는 스크립트를 작성 중입니다. 모든 열을 반복하고 정규식 정규식 일치를 새 문자열로 반복합니다. 열 이름을 줄이는 데 잘 작동하고 편리합니다. 숫자로 시작하는 문자열로 바꾸는 경우를 제외하고는 역참조가 엉망이 되기 때문입니다(아래의 경우 \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는 시작 및 끝 위치와 일치합니다.