Estoy escribiendo algo para reemplazar los nombres de las columnas según una coincidencia de expresiones regulares. Repito todas las columnas y las expresiones regulares coinciden con una nueva cadena. Funciona bien y es útil para acortar nombres de columnas, excepto cuando reemplazo una cadena que comienza con un número; esto se debe a que la referencia inversa se estropea (\1 se convierte en \133 en el caso siguiente)
En Python, existe una "referencia inversa no ambigua" para solucionar este problema. ¿Ocurre lo mismo en JSL? Leí en alguna parte que tal vez usa referencias retrospectivas de tipo Perl, pero no pude encontrar dónde está documentado.
Gracias,
Durazno
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.
Edición: La función regex() tiene dos parámetros que usan escapes. El parámetro 2 es el patrón y usa los escapes como la mayoría de la gente espera, y los \Q \U \L \E \u \l no pertenecen a este parámetro. El parámetro 3 es el reemplazo, y los \Q \U \L \E \u \l sí se pueden usar. Las retrorreferencias \1 \2 \3 ... se pueden usar tanto en parm2 como en parm3.
Edición 14 ago. 2025: De hecho, \U\u\L\l se puede usar en el parámetro 2 y coincidir con un solo carácter: u mayúscula, U no mayúscula, l minúscula y L no minúscula. Además, \A y \Z coinciden con las posiciones inicial y final.