<?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: Perl RegEx - take the shortest possible phrase until a specific character in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/830241#M328052</link>
    <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Thank you! "/1514[^Q\$]*Q[^\$]*\$/i" pattern worked.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Would you mind explaining why replacement of dots to "[^Q\$]" and "[^\$]" worked?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Sorry for the late response. I had so many work assignments I did not find the time for the code.&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Aug 2022 08:08:53 GMT</pubDate>
    <dc:creator>Jedrek369</dc:creator>
    <dc:date>2022-08-25T08:08:53Z</dc:date>
    <item>
      <title>Perl RegEx - take the shortest possible phrase until a specific character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/827366#M326816</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I would like to derive "1514#0_2021Q3_2022Q1$" substring&amp;nbsp;&lt;FONT face="Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace" color="#000000"&gt;&lt;SPAN&gt;from the following string:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace" color="#000000"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;1514#0 Deep Dive$   1514#0_2021Q3_2022Q1$   1515#0 Deep Dive$   1515#0_2021Q1_2022Q1$   XYZ$  Dictionary$&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The following conditions need to be met: 1) the substring has at least one "Q" letter, 2) substrings in the list are ordered randomly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used the following pattern, but it fell short:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/1514.*?Q.*?\$?/i&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the code for your reference:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro prxsubstr(pattern, string);
	%let regex_id = %sysfunc(prxparse(&amp;amp;pattern.));
	%let position = 0;
	%let length = 0;
	%syscall prxsubstr(regex_id, string, position, length);
	%global substring;
	%let substring = %substr(&amp;amp;string., &amp;amp;position., &amp;amp;length.);
	%put &amp;amp;substring.;
%mend;

%let pattern = /1514.*?Q.*?\$?/i;
%let list = 1514#0 Deep Dive$   1514#0_2021Q3_2022Q1$   1515#0 Deep Dive$   1515#0_2021Q1_2022Q1$   ARS_CRS KPI Results$  Dictionary$;

%prxsubstr(&amp;amp;pattern., &amp;amp;list.);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help will be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 14:10:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/827366#M326816</guid>
      <dc:creator>Jedrek369</dc:creator>
      <dc:date>2022-08-05T14:10:55Z</dc:date>
    </item>
    <item>
      <title>Re: Perl RegEx - take the shortest possible phrase until a specific character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/827425#M326842</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343368"&gt;@Jedrek369&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the "specific character" mentioned in the subject line the dollar sign? If so, why do you make it optional in your pattern?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343368"&gt;@Jedrek369&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;/1514.*?Q.*?\$&lt;FONT color="#FF0000"&gt;?&lt;/FONT&gt;/i&lt;/CODE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Perhaps because a missing dollar sign would be acceptable at the end of the list? In this case I would put&lt;/P&gt;
&lt;PRE&gt;(\$|$)&lt;/PRE&gt;
&lt;P&gt;to the end of the pattern (where the first "$" is the dollar sign and the second the end-of-string mark).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it would help to replace the periods (.) in your pattern with "negative character sets" (see &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0s9ilagexmjl8n1u7e1t1jfnzlk.htm#p12dxk6qpmhaddn11dk5jo73i9h4" target="_blank" rel="noopener"&gt;Class Groupings&lt;/A&gt;) excluding "Q" or "$" or both:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;/1514&lt;FONT color="#3366FF"&gt;[^Q\$]&lt;/FONT&gt;*Q&lt;FONT color="#3366FF"&gt;[^\$]&lt;/FONT&gt;*\$/i&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;(Also, would a leading word boundary (&lt;FONT face="courier new,courier"&gt;\b&lt;/FONT&gt;) help or would "&lt;FONT face="courier new,courier"&gt;16&lt;STRONG&gt;1514&lt;/STRONG&gt;&lt;/FONT&gt;..." be fine?)&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2022 18:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/827425#M326842</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-05T18:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: Perl RegEx - take the shortest possible phrase until a specific character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/830241#M328052</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Thank you! "/1514[^Q\$]*Q[^\$]*\$/i" pattern worked.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Would you mind explaining why replacement of dots to "[^Q\$]" and "[^\$]" worked?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3" color="#000000"&gt;Sorry for the late response. I had so many work assignments I did not find the time for the code.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 08:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/830241#M328052</guid>
      <dc:creator>Jedrek369</dc:creator>
      <dc:date>2022-08-25T08:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: Perl RegEx - take the shortest possible phrase until a specific character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/830248#M328054</link>
      <description>&lt;P&gt;You're welcome. Glad to hear that it worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are three fixed parts in your pattern: the "1514", at least one "Q" and the "$" sign -- in this order, with the dollar sign ending the pattern. Between the "1514" and the &lt;EM&gt;first&lt;/EM&gt; "Q" almost any characters are allowed, but a "Q" is logically impossible because then &lt;EM&gt;this&lt;/EM&gt;&amp;nbsp;would be the first "Q" and a "$" sign is not acceptable either as it would indicate the end of a string &lt;EM&gt;not&lt;/EM&gt; containing a "Q". This explains why&amp;nbsp;"Q" and "$" had to be excluded in that place. The greedy "*" repetition factor could be used with the &lt;FONT face="courier new,courier"&gt;[^Q\$]&lt;/FONT&gt; pattern because the search would stop at the "Q" (but not earlier) anyway.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Between the first "Q" and the closing "$", again, almost any characters are allowed (this time including "Q"), but logically not a (prematurely closing) "$" sign. This explains why I used &lt;FONT face="courier new,courier"&gt;[^\$]&lt;/FONT&gt; there. As above, the greedy&amp;nbsp;"*" repetition factor is possible because of the explicit "$" closing the pattern. Alternatively, however, you could leave your original &lt;FONT face="courier new,courier"&gt;.*?&lt;/FONT&gt; in that place because the lazy "&lt;FONT face="courier new,courier"&gt;*?&lt;/FONT&gt;" repetition factor would prevent the pattern from including another "$" sign.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2022 09:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/830248#M328054</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-25T09:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: Perl RegEx - take the shortest possible phrase until a specific character</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/831086#M328418</link>
      <description>&lt;P&gt;Thank you! Now it is clear.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2022 12:49:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perl-RegEx-take-the-shortest-possible-phrase-until-a-specific/m-p/831086#M328418</guid>
      <dc:creator>Jedrek369</dc:creator>
      <dc:date>2022-08-30T12:49:26Z</dc:date>
    </item>
  </channel>
</rss>

