BookmarkSubscribeRSS Feed
ChrisNZ
Tourmaline | Level 20

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.

3 REPLIES 3
Tom
Super User Tom
Super User

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)));

 

ChrisNZ
Tourmaline | Level 20

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.

ChrisNZ
Tourmaline | Level 20
Tech support as lodged a defect.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 1506 views
  • 0 likes
  • 2 in conversation