<?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 Regex finding optional characters in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/334933#M58250</link>
    <description>&lt;P&gt;I have text strings that can look like Str1 and Str2 below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Str1 = "ParamList:Param1,Param2,Param3";
Str2 = "ParamList:Param1,Param2,Param3;Var2;Var3";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So there's a comma-delimited list of values prefixed with a keyword and colon. The comma-delimited list might or might not be followed by additional variables. If it is, the comma-delimited list and subsequent variables will be separated by semi-colons as shown in Str2.&lt;/P&gt;&lt;P&gt;I want to grab the comma-delimited list. I've tried the following regex&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ParamLst1 = RegexMatch(Str2, "(ParamList:)(.*);?")[3];
ParamLst2 = RegexMatch(Str2, "(ParamList:)(.*);?")[3];&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm thinking it will find "ParamList:" then (.*) being greedy will match everything to the end of the string, but will back up until it finds zero or one semi-colons.&lt;/P&gt;&lt;P&gt;I must be thinking about it wrong, since with Str2, I get everything back to the end of the string ( "Param1,Param2,Param3;Var2;Var3")&lt;/P&gt;&lt;P&gt;I can use the word function to bail myself out :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;word(1,ParamLst2 = RegexMatch(Str2, "(ParamList:)(.*)")[3],";");&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and this works for both Str1 and Str2, but is adding complexity that I'm sure is unnecessary if I could grasp regex a little better !&lt;/P&gt;&lt;P&gt;Any ideas ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 21:58:24 GMT</pubDate>
    <dc:creator>tsl</dc:creator>
    <dc:date>2023-06-09T21:58:24Z</dc:date>
    <item>
      <title>Regex finding optional characters</title>
      <link>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/334933#M58250</link>
      <description>&lt;P&gt;I have text strings that can look like Str1 and Str2 below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Str1 = "ParamList:Param1,Param2,Param3";
Str2 = "ParamList:Param1,Param2,Param3;Var2;Var3";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So there's a comma-delimited list of values prefixed with a keyword and colon. The comma-delimited list might or might not be followed by additional variables. If it is, the comma-delimited list and subsequent variables will be separated by semi-colons as shown in Str2.&lt;/P&gt;&lt;P&gt;I want to grab the comma-delimited list. I've tried the following regex&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;ParamLst1 = RegexMatch(Str2, "(ParamList:)(.*);?")[3];
ParamLst2 = RegexMatch(Str2, "(ParamList:)(.*);?")[3];&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm thinking it will find "ParamList:" then (.*) being greedy will match everything to the end of the string, but will back up until it finds zero or one semi-colons.&lt;/P&gt;&lt;P&gt;I must be thinking about it wrong, since with Str2, I get everything back to the end of the string ( "Param1,Param2,Param3;Var2;Var3")&lt;/P&gt;&lt;P&gt;I can use the word function to bail myself out :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;word(1,ParamLst2 = RegexMatch(Str2, "(ParamList:)(.*)")[3],";");&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and this works for both Str1 and Str2, but is adding complexity that I'm sure is unnecessary if I could grasp regex a little better !&lt;/P&gt;&lt;P&gt;Any ideas ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 21:58:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/334933#M58250</guid>
      <dc:creator>tsl</dc:creator>
      <dc:date>2023-06-09T21:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Regex finding optional characters</title>
      <link>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/335077#M58262</link>
      <description>&lt;P&gt;Here's a simple brute force approach using JSL's string functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Str1 = "ParamList:Param1,Param2,Param3";
Str2 = "ParamList:Param1,Param2,Param3;Var2;Var3";

s1 = substr(str1, 11);
w1 = words(s1, ";");
plist1 = words(w1[1], ",");

s2 = substr(str2, 11);
w2 = words(s2, ";");
plist2 = words(w2[1], ",");&lt;BR /&gt;show(plist1, plist2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;PRE&gt;plist1 = {"Param1", "Param2", "Param3"};
plist2 = {"Param1", "Param2", "Param3"};&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Nov 2020 14:38:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/335077#M58262</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2020-11-18T14:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Regex finding optional characters</title>
      <link>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/335097#M58264</link>
      <description>&lt;P&gt;Or, using regex&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Str1 = "ParamList:Param1,Param2,Param3";
Str2 = "ParamList:Param1,Param2,Param3;Var2;Var3";

regex(Str2, "^([^:]*):([^;]*);?(.*)$", "part1='\1' part2='\2' part3='\3'" )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;part1='ParamList' part2='Param1,Param2,Param3' part3=''&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;or&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;part1='ParamList' part2='Param1,Param2,Param3' part3='Var2;Var3'&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The beginning ^ and ending $ force the regex to match from start to finish. The [^:] matches characters that are not : and [^;] matches characters that are not ; . The lone : gets past the required : and the ;? is an optional ; .&amp;nbsp; The .* matches the remaining characters, if any.&lt;/P&gt;&lt;P&gt;The parens make capturing groups. The groups are referred to in the 3rd parameter as \1, \2, and \3 and inserted into a string that makes the result.&lt;/P&gt;&lt;P&gt;Note that all of the * operators are written so they never go "too far" and need to back up. This makes matching efficient. Use the negative character classes when you can to match everything up to a delimiter.&lt;/P&gt;&lt;P&gt;The result string could be "\2" if that's all you want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 14:51:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Regex-finding-optional-characters/m-p/335097#M58264</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2020-11-18T14:51:46Z</dc:date>
    </item>
  </channel>
</rss>

