模式匹配你好,
我正在尝试替换部分文本,希望得到代码方面的帮助。
我注意到使用旧的列名语法运行 jsl 会影响运行时间。
我想制作一个脚本,它采用我当前的 jsl 代码并将其从以下内容进行更改:
:Name( "col_name" )
对此:
:"col_name"n
我尝试使用 Regex 和 Patt Match,但没有成功。
有人知道怎么做吗?
自动化和脚本回复:模式匹配您是否尝试使用搜索脚本执行此操作?

更换后

:Name\( (.+?) \)
:\1n
回复:模式匹配不,我正在尝试使用 Regex 或 Patt Match 函数做完全相同的事情。
回复:模式匹配T/S 几天后,我发现没有办法在 jsl 中做到这一点,而不会丢失被重写的 jsl 代码中的注释。
所以我用python写了一个代码来做这件事
# 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)回复:模式匹配如果你有这样的脚本
Names Default To Here(1);
// this is comment
:Name("col_name");
:Name("col_name2");
您绝对可以替换那些 :Name("colname") 类型的字符串,而无需删除注释。
脚本.jsl

Script2.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);
回复:模式匹配...完整的脚本可能如下所示
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.
*/