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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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