BookmarkSubscribeRSS Feed
frisco
Calcite | Level 5

Hi, I need to parse a comment field that has TWO sets of quoted phrases - the kicker is that I only need the contents of the SECOND quoted phrase.

For example:

Blah blah Blah "Dont need this" yada yada yada "Definately need this" so forth and so on...

Is this possible using Perl regular expressions?

TIA.

4 REPLIES 4
polingjw
Quartz | Level 8

Here is an easy solution that does not use regular expressions.

data test;

comment = 'Blah blah Blah "Dont need this" yada yada yada "Definately need this" so forth and so on...';

quote=scan(comment, -2, '"');

run;

DLing
Obsidian | Level 7

Here's a code snippet that should work, typo notwithstanding.  Modify to suite your needs.

data _null_;

     if _n_ = 1 then do;

          regid = prxparse( '/^.*"(.*)".*"(.*)"/' );

          retain regid;

          if regid = 0 then do;

               put 'improper reg expression';

               stop;

          end;

     end;

     length str s1 s2 $ 1000;   /* or whatever length  */

     input str;

     s1 = prxposn( regid, 1, str );               /*  quick & short  */

     s2 = prxposn( regid, 2, str );               /*  believed to do two independent scans of the str  */

     /*  alternative form - believed to be more efficient as prxmatch is called only once  */

     if prxmatch( regid, str ) then do;     /*  if patterns matches then...  */

          call prxposn( regid, 1, pos, len );

          s1 = substr( str, pos, len );

          call prxposn( regid, 2, pos, len );

          s2 = substr( str, pos, len );

     end;

cards;

Blah blah Blah "Dont need this" yada yada yada "Definately need this" so forth and so on...

;;;;

The regular expression '/^.*"(.*)".*"(.*)"/' deconstructs as follows:

/ is the beginning of the regular expression.  standard convention.

^ is the beginning of the string

.* matches any character 0 or more times (indeterminate)

" matches this character - beginning of 1st quoted string

(.*) is the 1st capture buffer, marked by ().  the content is between the (), in this case .* says everything.

" finishes the 1st quotation

.* says anything can separate the two quoted strings

"(.*)" is the capture buffer for the second quoted string.

/ closes off this regular expression

Hope this helps.  Enjoy.

Ksharp
Super User

Agree with polingjw . Since you want the second part of quote.

data test;

comment = 'Blah blah Blah "Dont need this" yada yada yada "Definately need this" so forth and so on...';

quote=scan(comment, 4 , '"','m');

run;

Ksharp

Message was edited by: xia keshan

manojinpec
Obsidian | Level 7

Thanks Everybody.. Even i din't know about the modifier in scan function. Smiley Happy

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 942 views
  • 0 likes
  • 5 in conversation