cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

Pattern Match

ADaskalo
Level II

Hi,

 

I'm trying to replace part of text and would appreciate help with the code.

I noticed that running jsl with old column name syntax takes a toll on running time.

I would like to make script a that takes my current jsl code and change it from this:

:Name( "col_name" )

To this:

:"col_name"n

I tried using Regex and Patt Match with no luck.

Does anyone know how to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User


Re: Pattern Match

If you have a script like this

Names Default To Here(1);

// this is comment
:Name("col_name");

:Name("col_name2");

You can definitely replace those :Name("colname") type of strings without removing the comments.

Script.jsl

jthi_0-1742738733353.png

Script2.jsl

jthi_1-1742738757585.png

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);

 

-Jarmo

View solution in original post

5 REPLIES 5
jthi
Super User


Re: Pattern Match

Are you trying to do this with Search Script?

jthi_1-1742391219174.png

After replace

jthi_2-1742391237889.png

 

:Name\( (.+?) \)
:\1n
-Jarmo
ADaskalo
Level II


Re: Pattern Match

No, I'm trying to do the exact same thing with Regex or Patt Match functions.

 

 

ADaskalo
Level II


Re: Pattern Match

After T/S for a few days I found there's no way to do this in jsl w/o losing the comments within the jsl code being rewritten.

So I wrote a code in python that do that instead

 

# 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)
jthi
Super User


Re: Pattern Match

If you have a script like this

Names Default To Here(1);

// this is comment
:Name("col_name");

:Name("col_name2");

You can definitely replace those :Name("colname") type of strings without removing the comments.

Script.jsl

jthi_0-1742738733353.png

Script2.jsl

jthi_1-1742738757585.png

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);

 

-Jarmo
Craige_Hales
Super User


Re: Pattern Match

...and the complete script might look like this

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.
*/
Craige