패턴 매치안녕,
텍스트 일부를 바꾸려고 하는데 코드에 대한 도움을 주시면 감사하겠습니다.
이전의 열 이름 구문으로 jsl을 실행하면 실행 시간이 많이 길어진다는 것을 알았습니다.
현재 jsl 코드를 가져와서 다음과 같이 변경하는 스크립트 a를 만들고 싶습니다.
:Name( "col_name" )
이에 대하여:
:"col_name"n
Regex와 Patt Match를 사용해 보았지만 아무 소용이 없었습니다.
이걸 하는 방법을 아는 사람 있나요?
자동화 및 스크립팅Re: 패턴 매치이 작업을 검색 스크립트로 수행하려고 하시나요?

교체 후

:Name\( (.+?) \)
:\1n
Re: 패턴 매치아니요, Regex나 Patt Match 함수로 똑같은 일을 하려고 합니다.
Re: 패턴 매치며칠 동안 T/S를 진행한 결과, jsl 코드 내에서 주석을 잃지 않고는 jsl에서 이 작업을 수행할 방법이 없다는 것을 알게 되었습니다.
그래서 저는 대신 파이썬으로 그 작업을 하는 코드를 썼습니다.
# This code will replace code in .jsl files that match the Regex in parameters dictionary (pattern(1) and repl(2)) below.
# It is based on the simple def replace(source, dest) function, with pattern now being the source and the repl as the dest.
# The pattern in this code is to replace old JMP column naming syntax to the new JMP column naming syntax,
# e.g :Name( "variable@property" ) into :"variable@property"n
# for jsl code to run more efficiently in JMP17 and above
import os
import re
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
WORKING_DIR = filedialog.askdirectory()
# pattern(1)
pattern = r':\s*Name\s*\(\s*"([^"]+)"\s*\)'
# repl(2)
repl = r':"\1"n'
parameters = {pattern: repl}
def pat_replace(pat, rep):
for dname, dirs, files in os.walk(WORKING_DIR):
for fname in files:
if fname.endswith(".jsl"):
fpath = os.path.join(dname, fname)
with open(fpath, encoding="utf8") as f:
s = f.read()
s_out = re.sub(pat, rep, s)
with open(fpath, "w", encoding="utf8") as f:
f.write(s_out)
for i, (key, value) in enumerate(parameters.items()):
pat_replace(key, value)Re: 패턴 매치이와 같은 스크립트가 있는 경우
Names Default To Here(1);
// this is comment
:Name("col_name");
:Name("col_name2");
주석을 제거하지 않고도 :Name("colname") 유형의 문자열을 바꿀 수 있습니다.
스크립트.jsl

스크립트2.jsl

Names Default To Here(1);
path_to_file = "$DOWNLOADS/Script.jsl";
path_to_new_file = "$DOWNLOADS/Script2.jsl";
str = Load Text File(path_to_file);
new_str = Regex(str, ":Name\( (.+?) \)", ":\1n", GLOBALREPLACE);
// Write(new_str);
Save Text File(path_to_new_file, new_str);
Re: 패턴 매치...그리고 전체 스크립트는 다음과 같을 것입니다.
WORKING_DIR = Pick Directory( "Select a directory" );
pattern = "\[:\s*Name\s*\(\s*"([^"]+)"\s*\)]\";
rep = "\[:"\1"n]\";
pat_replace = Function( {pat, rep},
For Each( {f}, Files In Directory( WORKING_DIR, recursive(1) ),
If( Ends With( f, ".jsl" ),
f = WORKING_DIR || f;
text = Load Text File( f );
newtext = Regex( text, pat, rep, GLOBALREPLACE );
If( newtext != text,
Save Text File( f || ".new.jsl", newtext );
);
)
)
);
pat_replace( pattern, rep );
/*
the "\[...]\" escape in JSL is NOT a perfect replacement for the
python r-string, though it does work for this example. In particular,
if the regex contains ]\ it will not work out without more escaping
magic.
The file is renamed when saved so if something goes wrong...you could
save to another directory, etc.
Recursive(1) includes subdirs, change as needed.
*/