<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Pattern Match in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849288#M102534</link>
    <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;So I wrote a code in python that do that&amp;nbsp;instead&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;# 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)&lt;/PRE&gt;</description>
    <pubDate>Sun, 23 Mar 2025 12:00:30 GMT</pubDate>
    <dc:creator>ADaskalo</dc:creator>
    <dc:date>2025-03-23T12:00:30Z</dc:date>
    <item>
      <title>Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848527#M102394</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to replace part of text and would appreciate help with the code.&lt;/P&gt;&lt;P&gt;I noticed that running jsl with old column name syntax takes a toll on running time.&lt;/P&gt;&lt;P&gt;I would like to make script a that takes my current jsl code and change it from this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;:Name( "col_name" )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;:"col_name"n&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried using Regex and Patt Match with no luck.&lt;/P&gt;&lt;P&gt;Does anyone know how to do this?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 13:09:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848527#M102394</guid>
      <dc:creator>ADaskalo</dc:creator>
      <dc:date>2025-03-19T13:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848552#M102397</link>
      <description>&lt;P&gt;Are you trying to do this with Search Script?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1742391219174.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74001i199F9DAEF5CA05BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_1-1742391219174.png" alt="jthi_1-1742391219174.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;After replace&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_2-1742391237889.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74002i88C0C69AAC28A167/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_2-1742391237889.png" alt="jthi_2-1742391237889.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;:Name\( (.+?) \)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;:\1n&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Mar 2025 13:34:07 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848552#M102397</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-03-19T13:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848554#M102398</link>
      <description>&lt;P&gt;No, I'm trying to do the exact same thing with Regex or Patt Match functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 14:00:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/848554#M102398</guid>
      <dc:creator>ADaskalo</dc:creator>
      <dc:date>2025-03-19T14:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849288#M102534</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;So I wrote a code in python that do that&amp;nbsp;instead&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;# 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)&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Mar 2025 12:00:30 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849288#M102534</guid>
      <dc:creator>ADaskalo</dc:creator>
      <dc:date>2025-03-23T12:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849296#M102538</link>
      <description>&lt;P&gt;If you have a script like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

// this is comment
:Name("col_name");

:Name("col_name2");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can definitely replace those :Name("colname") type of strings without removing the comments. &lt;/P&gt;
&lt;P&gt;Script.jsl&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1742738733353.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74125i8CB878B06A9B8AFD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1742738733353.png" alt="jthi_0-1742738733353.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Script2.jsl&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1742738757585.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74126i00770540C03406F3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_1-1742738757585.png" alt="jthi_1-1742738757585.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 14:06:43 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849296#M102538</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-03-23T14:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Pattern Match</title>
      <link>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849298#M102540</link>
      <description>&lt;P&gt;...and the complete script might look like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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.
*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Mar 2025 14:36:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Pattern-Match/m-p/849298#M102540</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2025-03-23T14:36:09Z</dc:date>
    </item>
  </channel>
</rss>

