Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
rajasekhar_reddy14
Active Contributor
0 Kudos

This document will help PI developers to know about JAVA Regular expressions usage in PI Mappings.

 

Regex completely JAVA concept, so whoever knows java they can understand it very easily.

What is Regex:

  

Java provides the java.util.regex package for pattern matching with regular expressions. 

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.

The java.util.regex package primarily consists of the following three classes:

 

  • Pattern Class: A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument.

 

  • Matcher Class: A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher method on a Pattern object.

 

  • PatternSyntaxException: A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

More information refer below link

http://download.oracle.com/javase/tutorial/essential/regex/

Where we use Regex in PI:

 

Regex help us to achieve complex mapping logisc by writing few lines of code in UDF’s and JAVA Mapping.

It is very common requirement in PI to manipulation of strings, remove special characters, white spaces and name spaces in XML document.

The main advantage of regex was it avoids using number standard functions, every standard function associated with JAVA code, so if you use much standard function to achieve your requirement ultimately it created burden on server.

So best practice is writing JAVA code with good standards and minimum number of lines always right idea.

Use Case1:

 

Does a  string contain a series of 10 digits?, if you want write this logic in PI required few standard function or few lines of  JAVA code in UDF,but using regex it  is very simple

 

public boolean hasTenDigits(String s) {

  return s.matches(".*[0-9]{10}.*");

}

 

Use Case2:

 

If you just want to replace all instances of a given expression within a string with another fixed string, then things are fairly straightforward. For example, the following replaces all instances of digits with a letter Z:

str = str.replaceAll("[0-9]", "X");

 

Use Case3:

It is possible to use a regular expression to split or tokenise a string, with similar but more flexible functionality to that of StringTokenizer. To split a string, we can write a line such as the following.

 

String[] words = str.split("\\s+");

 

Use Case4:

Using below lines of code we can remove training white spaces in string.

 

public  String removeTrailingWhiteSpaces(String str) {

            str = str.replaceAll("(?s)([ \t]*)$", "");

            str = str.replaceAll("\r\n", "\n");

            str = str.replaceAll("\r", "\n");

            str = str.replaceAll("(?s)([ \t]*)\n", "\n");

            str = str.replaceAll("(?s)([\n]*\n)", "\n");

            return str;

      }

 

Use Case5:

Removing new lines, name spaces in XML document below piece of code will work.

 

public  String normalizeXML(String xml) {

               if (xml.indexOf("= 0)

      xml = xml.replaceFirst("(?s)(.*?)()(.*)", "$5");

            // remove namespaces from xml documents.

            if (xml.indexOf("xmlns") > 0)

             {

String regex = "(?s)(xmlns[a-zA-Z0-9:$,.\\-&_~*!';/()@?=+;%#]*=\"[a-zA-Z0-9:$,.\\-&_~*!';/()@?=+;%#]*?\")";

      xml = xml.replaceAll(regex,"" );

             }

       xml = xml.replaceAll("([A-Za-z0-9]*>)([ \\n\\r\\t]*?)(<[A-Za-z0-9]*)", "$1$3");

            xml = xml.replaceAll("(?s)(.*?)([ \\n\\r\\t]*$)", "$1");

            return xml;

      }

 

More Regex examples refer below link

http://www.java2s.com/Code/Java/Regular-Expressions/JavaRegularExpressionSplittext.htm

PS:

Avoid using excessive regex code in PI mappings(UDF’s and JAVA Mapping),the reason was regex leads some time performance issues and always follow good programming standards while writing UDF’s and JAVA Mappings.

Please check your regex code before testing in PI Mappings, for this use regex coach tool.

http://the-regex-coach.en.softonic.com/

2 Comments