Using regular expressions in a routing rule

I would like to examine the contents of my OBX-5 field and not route the message if it contains alphabetic characters. I've tried various combinations of the Match and Contains functions, with no luck. Should I be using the COS ? operator or plaini regular expressions?

e.g.

OBX-5 Contains "\D"

OBX-5 Contains "?.A"

OBX-5 Contains "[A-Z]"

  • 0
  • 0
  • 64
  • 7
  • 2

Answers

If  you expect to be using regular expressions frequently, it might be worth your while to write a classmethod that extends Ens.Rule.FunctionSet and wraps $MATCH or $LOCATE in something that can be called from a business rule (or DTL for that matter).

/// Custom methods for business rules and DTLs
Class User.Rule.FunctionSet Extends Ens.Rule.FunctionSet
{

/// Accepts a string <var>pString</var> and regular expression pattern <var>pPattern</var>
/// as arguments; returns 0 for no match, and a positive integer indicating the match's
/// position if there is a match.
ClassMethod REMatch(pString As %String, pPattern As %String) As %Integer
  {
              Return $LOCATE(pString,pPattern)
  }

}

You could use $MATCH instead of $LOCATE in the method above, but $MATCH() assumes the supplied pattern is begin- and end-anchored. In other words, $LOCATE("this","is") returns the positive integer 3, which for all Cache boolean purposes evaluates as true. $MATCH("this","is") returns 0 (false) since "^is$" does not match "this".

If the class you create extends Ens.Rule.FunctionSet and resides in your production's namespace, it will be selectable from the function dropdown list in both the Business Rule and DTL expression editors.

If you're really set on using the ? notation for pattern matching, see the "ObjectScript Pattern Matching" section of the documentation. In the pattern field of the Match function in the expression editor, you would enter the pattern without a leading question mark. For example, you can use the string ".A" to match any number of upper or lowercase characters, or ".AP" to match any number of upper/lowercase and punctuation characters.

Comments

What do you mean you're not having luck? For example, what happens when you call on the $match or $locate functions on your field? I can make regex on your three examples without issue for simple strings, like below which works:

$locate("123dsd534","[a-z]")

This is in a routing rule, so when I have:

when HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches "[a-z]"

and when I run a message through I get the following error:

 ERROR <Ens>ErrBPTerminated: Terminating BP iSirona_Proc2 # due to error: ERROR <Ens>ErrException: <SYNTAX>zMatches+1 ^Ens.Util.FunctionSet.1 -- logged as '-'
number - @'
Quit $S(""=$g(pattern):""=$g(value), 1:$g(value)?@$g(pattern)) }'
> ERROR <Ens>ErrException: <SYNTAX>zMatches+1 ^Ens.Util.FunctionSet.1 -- logged as '-'
number - @'
Quit $S(""=$g(pattern):""=$g(value), 1:$g(value)?@$g(pattern)) }'

the Matches method uses the ? (question mark) syntax for pattern matching, not regular expressions. I much prefer the latter ...

Any hints on the syntax? I've tried

HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches ?3N

HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches ?"-"

The following: HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches "?.A"

gives me the following error:

 ERROR <Ens>ErrBPTerminated: Terminating BP iSirona_Proc2 # due to error: ERROR <Ens>ErrException: <SYNTAX>zMatches+1 ^Ens.Util.FunctionSet.1 -- logged as '-'
number - @'
Quit $S(""=$g(pattern):""=$g(value), 1:$g(value)?@$g(pattern)) }'
> ERROR <Ens>ErrException: <SYNTAX>zMatches+1 ^Ens.Util.FunctionSet.1 -- logged as '-'
number - @'
Quit $S(""=$g(pattern):""=$g(value), 1:$g(value)?@$g(pattern)) }'

Oops! Sorry I forgot it was for a rule. Also edited for redundancy as I see there's more comments now. Does the visual expression editor give any indication of failure?

The following attempts give me a syntax error "Error parsing expression"  when I save the rule:

HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches ?3N

HL7.{OBXgrp(1).OBX:ObservationValue(1)} Matches ?"-"

HL7.{OBXgrp(1).OBX:ObservationValue(1)}?"-"

HL7.{OBXgrp(1).OBX:ObservationValue(1)} ?"-"

Are you wrapping your expression in quotes? This indicates that Matches only accepts strings as the pattern. If that doesn't work I'm really not sure what the issue is, and I would try to pass your values to a custom function where you can examine what's really going on, and see if you can replicate the problem. This is essentially what Jeffrey Drumm was suggesting.