Search code examples
regexsassas-wps

SAS Using $ in Regex


I have a load of ID's that I want to flag if it ends in a slash and one or two digits. (/00, /01, /0)

Some ID's contain slashes in the middle that I don't want to flag.

So I want my result to be:

ID                Flag
P-MTP00197854/03    X
P-MTP00197868/02    X
P-MTP00197882/00    X
P-HMF00197730   
WI/B09/077094   
Q-MTP00197735   
Q-HMF00199313/0     X

I'd expect this function to do the job, but it doesn't detect anything:

if prxmatch("/\/\d{1,2}$/", ID) then Flag='X';

If I remove the $ sign, it detects what I want, but it also includes those ID's with slashes in the middle as it isn't starting at the end.

I've tested it on regex101 and it should work. Am I missing something here?


Solution

  • To prevent a partial match, you can use a word boundary. To make sure that there is no more / to the right you can use a positive lookahead asserting not more occurrences of / to the right

    \/\d{1,2}\b(?=[^\r\n\/]*$)
    

    See a regex demo.

    Or the other way around with a negative lookahead, asserting no / to the right:

    \/\d{1,2}\b(?![^\/\r\n]\/)
    

    See another regex demo.