Database Reference
In-Depth Information
different parts, and pretty much work with anything that involves looking through,
processing, or modifying strings of even the slightest complexity. regular expressions
allow us to do these things without engineering custom code on a case-by-case basis.
We identify the patterns we are dealing with and let the regex engine do the hard work.
Even a minor exploration of regexes is more than we can cover in this topic. here
are, in fact, entire topics devoted to the subject. All we can ofer is a little teaser. his
program uses two regular expression substitutions. The first is simple. It replaces spaces
in the topic title with underscores. he second moves the author's last name to the end
and removes the comma.
class Book {String title; String author;}
book = new Book(title: "Ender's Game", author: "Card, Orson Scott")
underscoredName = book.title.replaceAll(/ /,'_')
firstNameFirst = book.author.replaceAll(/^([^,]+),\s+(.+)$/,'$2
$1')
println "My favorite book is _${underscoredName}_ by ${first
NameFirst}."
The result is: my favorite book is _Ender's_game_ by orson Scott Card.
In addition to this Java-style usage, groovy provides special regex operators.
if (testString =~ /\d+/) {println "Match!"} // works if the string
has any numbers
if (testString ==~ /\d+/) {println "Match!"}// works if the string
is all digits
regexes are a rich subject that reward exploration. They are featured in many lan-
guages, libraries, and tools, so they can be a valuable part of any developer's toolkit.
9.4.11 Random Acts of Grooviness
Space demands that we wrap up our discussion of the language itself pretty soon now, so
we will hit upon a few more subjects here rapid fire. In other words, many more things
are awesome about groovy, and here are the highlights:
Groovy truth —It is acceptable to use any object, variable, value, etc. anywhere that a
Boolean expression is expected. Empty collections, zero-length strings, nulls, the num-
ber 0, and things like that are interpreted as false. Pretty much everything else evaluates
to true. use this feature with caution, and remember to favor readability.
Safe dereferencing —here are times in our programming lives when we are not sure
if one of our object variables has a value, or if one of its methods could return a null. At
these times, we might write something like:
if ( obj != null && obj.property == whatever ) { do_something(); }
This keeps us from encountering a null pointer exception (nPE) for trying to access
property when obj is null. In groovy, we can use a special operator together with groovy
truth to write instead:if ( obj?.property == whatever ) { do_something(); }This is espe-
cially helpful when we need to go more than one property (or method) deep and be
careful of nPEs all along the way.
Optional exception handling —In Java, there are some exceptions that must be
checked when calling a method, and code must be written to handle them. From one
Search WWH ::




Custom Search