DATA Step, Macro, Functions and more

quotes in regexp in macros

Reply
PROC Star
Posts: 1,566

quotes in regexp in macros

Hi all,

I am validating values, looking for quoted single characters, except quoted quotes.

'a'  yes

'"'  no

'A   no

Abc  no

This works fine, but I had to fight the code to make it work.

Here b works, c doesn't.

%let a="'";

%put b=%sysfunc(prxmatch(/^(%str(%'|%"))[^%str(%"%')]\1$/,%superq(a)));

%put c=%sysfunc(prxmatch(/^(%str(%'|%"))[^%str(%'%")]\1$/,%superq(a)));

The only change is the order of the quotes in the regexp list expression. SAS wants them "closed" in the right order or gets confused.

This order should not matter (it is just a list), and does not matter in SAS code or when using prxparse:

%put d=%sysfunc(prxmatch(%sysfunc(prxparse(/^(%str(%'|%"))[^%str(%"%')]\1$/)),%superq(a)));

%put e=%sysfunc(prxmatch(%sysfunc(prxparse(/^(%str(%'|%"))[^%str(%'%")]\1$/)),%superq(a)));

I sent this to tech support, but was wondering whether anyone had encountered this / any comments / similar odd regexp cases.

Super User
Super User
Posts: 6,502

Re: quotes in regexp in macros

[ Edited ]

You are working too hard to create the regex expression. Let's break it down.  To match any character that is not a double quote you need [^"]. To match that surrounded by quotes you just need to add the quotes. To have that match the full string then you need at have ^ before and $ after.  Plus you need the beginning and ending slashes. Since your single quotes are balanced and your unbalanced double quote is inside of single quotes then no macro quoting of the regex is required.

 

 

%let pos=%sysfunc(prxmatch(/^'[^"]'$/,%superq(a)));

 

PROC Star
Posts: 1,566

Re: quotes in regexp in macros

[ Edited ]

Tom i think you missed the fact that the string can be singled quoted *or doubled quoted*. The double quote option was not obvious from my text though it was used in the second example.

PROC Star
Posts: 1,566

Re: quotes in regexp in macros

Tech support as lodged a defect.
Ask a Question
Discussion stats
  • 3 replies
  • 187 views
  • 0 likes
  • 2 in conversation