| By ChiliJoe
Regular expressions (or regex as it is affectionately called) is a powerful tool for searching (and also, replacing) complex text patterns. A quick tutorial can be found at www.regular-expressions.info.[1] To quote from that site:
If you are a programmer, you can save yourself lots of time and effort. You can often accomplish with a single regular expression in one or a few lines of code what would otherwise take dozens or hundreds.
In addition to that argument, writing your own implementation with dozens of lines of code also makes it prone to defects, and makes it more difficult to maintain. And if the required pattern is changed slightly, the impact on your code could be very huge, or even require a rewrite of your custom implementation. With regex, what is often required is to change a few characters in the pattern.[2]
PeopleCode do not have an implementation of regular expressions, but fortunately, it is easy to utilize Java objects from PeopleCode. Note that regex capabilities was only added to Java since version 1.4.
www.regular-expressions.info has a short overview of the standard Java regex usage (Note: you can ignore the reminder about escaping backslashes, since the literals will be written in PeopleCode). As you can see, the Java String object itself has some regex capabilities. For the most common operations (match, replace first, replace all, split), using a Java String object can be sufficient.
The following PeopleCode example shows how to check if a page field value is a valid email address. It uses the email address pattern from www.regular-expressions.info, with the slight modification where “A-Z” is replaced with “a-zA-Z”.
Local JavaObject &email_regex = CreateJavaObject("java.lang.String", "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}\b");
Local JavaObject &test_string = CreateJavaObject("java.lang.String", PAGE_RECORD.TEST_FIELD);
If &test_string.matches(&email_regex) Then
/* PAGE_RECORD.TEST_FIELD is a valid email address */
End-If;
Using CreateJavaObject, we’ve instantiated a Java String object inside PeopleCode. Now we can use its matches method to test a regex match.
The following PeopleCode example shows how to replace all email addresses within a page field value to nospam@example.com.
Local JavaObject &email_regex = CreateJavaObject("java.lang.String", "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}\b");
Local JavaObject &test_string = CreateJavaObject("java.lang.String", PAGE_RECORD.LONGCHAR_TEST_FIELD);
PAGE_RECORD.LONGCHAR_TEST_FIELD = &test_string.replaceAll(&email_regex, "nospam@axample.com");
But that wasn’t quite so useful, isn’t it? Using back references, we can transform all email address with a string to email@addr.
Local JavaObject &email_regex = CreateJavaObject("java.lang.String", "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}\b");
Local JavaObject &test_string = CreateJavaObject("java.lang.String", PAGE_RECORD.LONGCHAR_TEST_FIELD);
PAGE_RECORD.LONGCHAR_TEST_FIELD = &test_string.replaceAll(&email_regex, "$0");
As you may have noticed, the replaceAll method returns a Java String object. PeopleCode automatically converts the value to a native PeopleCode string when saving to PAGE_RECORD.LONGCHAR_TEST_FIELD.
There are some limitations in using the Java String object. For one, you cannot set regex options – such as case-insensitive matching. In those cases, you’ll need to use a combination of Pattern & Matcher objects. You would also use these if you need the advanced capabilities of the Matcher class. Following is an example of the above operation using Pattern & Matcher objects.
Local JavaObject &javaPattern = GetJavaClass("java.util.regex.Pattern");
Local JavaObject &email_regex = CreateJavaObject("java.lang.String","\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}\b");
Local JavaObject &Regex_Pattern = &javaPattern.compile(&email_regex, &javaPattern.CASE_INSENSITIVE);
Local JavaObject &matcher = &Regex_Pattern.matcher(CreateJavaObject("java.lang.String", PAGE_RECORD.LONGCHAR_TEST_FIELD));
PAGE_RECORD.LONGCHAR_TEST_FIELD = &matcher.replaceAll("$0");
A more detailed tutorial on using the standard Java regex API can be found here.
http://java.sun.com/docs/books/tutorial/extra/regex/
[1]Warning: this site somewhat looks like an ad for PowerGrep and RegexBuddy, but it gives a nice and quick overview of regex.
[2]Caveat: Although a regular expression can be quite easy to write, it is often quite difficult to read. It is often a good idea to include a comment near your code explaining the purpose of each portion of the expression.
|