Sidebar

What is a regular expresion and is there a good tutorial and reference site online?

0 votes
499 views
asked Dec 17, 2013 by gary-t-8719 (14,860 points)

1 Answer

+2 votes

Regular expressions are a powerful way to find a variety of patterns or matching data in a string of text. The expression is built using symbols that represent the patterns or string of text you may be looking for.

For example, let’s say that you want to find the Order Number in the following NTE segment within an HL7 message

NTE|1|Please send order#123456-1 to the South Clinic|

To find this number within the text we can use an expression to symbolize the order number. We know that the order number is 6 digits long followed by a dash and then followed by another single digit. That pattern can be symbolized a couple of different way's as follows.

Example 1: [0-9]{6}-[0-9]{1}  

In this example we have told the regular expression validator to look for any string of text that has six {6} numbers (any number between 0 through 9) in a row followed by a dash - and then followed by only one {1} number (again any number 0 through 9). 

Example 2: \d{6}-\d{1} 

This example will find the exact same order number but instead of using the range of numbers [0-9] we are using the digit symbol \d

Within QIE I may want to extract this number out of this text and then place the number in a different field such as OBR-2. Here is how that code would look.

var stringToSearch = "NTE|1|Please send order#123456-1 to the South Clinic|";

var orderNum = /\d{6}-\d{1}/.exec(stringToSearch);

message.setNode('OBR-2', orderNum);

In the code above notice that we are using the JavaScript regular expression execute "exec()" method. This method starts with a forward slash followed by the regular expression and then ends with another forward slash and .exec(). The text we are searching is enclosed within the parenthesis which in this case is the variable stringToSearch.

The JavaScript language has 4 different types of regular expression methods. The two most commonly used methods that are used in QIE is the exec() and the test() methods. For more information about the 4 JavaScript methods visit http://www.w3schools.com/jsref/jsref_obj_regexp.asp

An excellent site for building and testing regular expressions is www.regexpal.com. Other regular expression test sites are also available but keep in mind that regular expressions can vary slightly with different languages. This site is based on the JavaScript language which is the same as QIE so regular expressions built in this tester will execute the same in QIE. This site also offers a Quick Reference that lists out the most common expressions or sysmbols. Look for the Quick Refernece guide in the upper right hand corner.

answered Jan 2, 2014 by gary-t-8719 (14,860 points)
edited Jan 2, 2014 by gary-t-8719
...