Daniel Lee · Feb 18, 2019

Pattern Matching with special characters

I am new to Intersystems Cache, so please bear with me. We are using HealthShare 2013.1. 

I have a routine to compare databases from separate instances where I want to ignore values that begin with %sqlcq. I thought this would be easy with pattern matching operator '?' but it is proving difficult and it seems to be because of the percent character. 

For example:

set x = ""

#;does not evaluate as true. 
if x?1P1"sqlcq" {write "valid"}

set y=""

if y?1"sqlcq".E {write "valid"}
if x?1"sqlcq".E {write "valid"}


If the variable value does not include the leading percent character, then I can successfully match. If it does include the percent character then it fails. 

I did try the following pattern, x?1P.1"sqlcq".E and I thought this was successful, but it ended up ignoring %SYS.FOO

What am I missing with pattern matching here? 

Thank you.

Dan Lee
Amazing Charts | Harris Computer

3 0 4 261
Log in or sign up to continue

I just found the $MATCH method which works. 

if $MATCH(p, "^%sqlcq.*"){
                    write "Skipping "_p,!

I find it simpler to use $Extract,
e.g. $E(x,1,7)="%sqlcq."

This seems to work for me:

set x = ""

write x?1"%sqlcq".E

That said, I assume you're iterating over some list of classes. If so, it may be better to add this or equivalent condition to the builder of the class list. If you do it via SQL and %Dictionary package, then excluding System classes, or generated classes or even classes that %STARTSWITH '%sqlcq' may be a better solution.

Maybe you want something like this:

set data1 = ""
set data2 = ""

for x=data1, data2 { if x ? .1"%"1"sqlcq".e { write "OK" } else { write "NOT" } w ! }

Your output should be: