Working with Text Strings and Scripting
Text processing is an area where PAW relies on the built-in capabilities of Java/Groovy to do complex processing when using the "Add calculated fields" feature.
Comparing Text
You can compare text directly with other text, be checking if a text is contained in another piece of text, or be using regular expressions to determine matches. There are a lot of articles on the web that provide detailed information on the use of regular expressions that we have provided below for reference.Comparing if 2 text strings are the same
def myText = "gouda-cheese"
def compareText = "gouda"
if (myText ==~ compareText) print "these strings are the same"
else print "these strings are different"
Result: these strings are different
Comparing if a text string contains another
def myText = "gouda-cheese"
def lookingForText = "gouda"
if (myText =~ lookingForText) print "this is gouda cheese"
else print "this cheese isn't gouda"
Result: this is gouda cheese
Composing Text
Text strings can be composed by joining together different strings together. You can use substitution as well as replacement.Basic text composition
def foodType = "cheese"
def myFavorite = "gouda"
def myFavoriteFood = myFavorite + " " + foodType
Value of myFavoriteFood: gouda cheese
Text composition with variables
def foodType = "cheese"
def myFavorite = "gouda"
def myFavoriteFood = "My favorite food is $myFavorite $foodType"
Value of myFavoriteFood: My favorite food is gouda cheese
Replacing text
def myDrinkTemplate = "My favorite drink is beer"
def myFavoriteBeverage = "wine"
def myDrink = myDrinkTemplate.replaceAll("beer", myFavoriteBeverage)
Value of myDrink: My favorite drink is wine
Parsing Text and Other Manipulation
Splitting text with a delimiter
def cheeses = "gouda,cheddar,jack,swiss"
def cheeseList = cheeses.split(",")
print cheeseList[0]
Result: gouda
Getting a part of the string
def myFavorites = "I like red wine and gouda cheese"
int end = myFavorites.indexOf(" cheese");
int start = myFavorites.lastIndexOf(" ", end);
print myFavorites.substring(start+1, end)
Result: gouda
Additional references
These articles on the web may change over-time and we cannot guarantee the continued accuracy of the information, but contain good additional tips.- Groovy Strings - http://groovy.codehaus.org/Strings
- String documentation for Java - http://java.sun.com/javase/6/docs/api/java/lang/String.html
- Groovy and Regular Expressions - http://groovy.codehaus.org/Regular+Expressions
- Regular expressions in Java - http://www.regular-expressions.info/java.html
