Sunday, May 28, 2017

Introduction to Java programming, Part 2 (8)

Regular expressions


regular expression is essentially a pattern to describe a set of strings that share that pattern. If you're a Perl programmer, you should feel right at home with the regular expression (regex) pattern syntax in the Java language. If you're not used to regular expressions syntax, however, it can look weird. This section gets you started with using regular expressions in your Java programs.

The Regular Expressions API

Here's a set of strings that have a few things in common:

  • A string
  • A longer string
  • A much longer string

Note that each of these strings begins with A and ends with string. The Java Regular Expressions API helps you pull out these elements, see the pattern among them, and do interesting things with the information you've gleaned.
The Regular Expressions API has three core classes that you use almost all the time:

  • Pattern describes a string pattern.
  • Matcher tests a string to see if it matches the pattern.
  • PatternSyntaxException tells you that something wasn't acceptable about the pattern that you tried to define.

You'll begin working on a simple regular-expressions pattern that uses these classes shortly. But first, take a look at the regex pattern syntax.

Regex pattern syntax

regex pattern describes the structure of the string that the expression tries to find in an input string. The pattern syntax can look strange to the uninitiated, but once you understand it, you'll find it easier to decipher. Table 2 lists some of the most common regex constructs that you use in pattern strings.
Table 2. Common regex constructs
Regex constructWhat qualifies as a match
.Any character
?Zero (0) or one (1) of what came before
*Zero (0) or more of what came before
+One (1) or more of what came before
[]A range of characters or digits
^Negation of whatever follows (that is, "not whatever")
\dAny digit (alternatively, [0-9])
\DAny nondigit (alternatively, [^0-9])
\sAny whitespace character (alternatively, [\n\t\f\r])
\SAny nonwhitespace character (alternatively, [^\n\t\f\r])
\wAny word character (alternatively, [a-zA-Z_0-9])
\WAny nonword character (alternatively, [^\w])
The first few constructs are called quantifiers, because they quantify what comes before them. Constructs like \d are predefined character classes. Any character that doesn't have special meaning in a pattern is a literal and matches itself.

Pattern matching

Armed with the pattern syntax in Table 2, you can work through the simple example in Listing 21, using the classes in the Java Regular Expressions API.
Listing 21. Pattern matching with regex
Pattern pattern = Pattern.compile("[Aa].*string");
  Matcher matcher = pattern.matcher("A string");
  boolean didMatch = matcher.matches();
  Logger.getAnonymousLogger().info (didMatch);
  int patternStartIndex = matcher.start();
  Logger.getAnonymousLogger().info (patternStartIndex);
  int patternEndIndex = matcher.end();
  Logger.getAnonymousLogger().info (patternEndIndex);
First, Listing 21 creates a Pattern class by calling compile()— a static method on Pattern— with a string literal representing the pattern you want to match. That literal uses the regex pattern syntax. In this example, the English translation of the pattern is:
Find a string of the form A or a followed by zero or more characters, followed by string.

Methods for matching

Next, Listing 21 calls matcher() on Pattern. That call creates a Matcher instance. The Matcher then searches the string you passed in for matches against the pattern string you used when you created thePattern.
Every Java language string is an indexed collection of characters, starting with 0 and ending with the string length minus one. The Matcher parses the string, starting at 0, and looks for matches against it. After that process is complete, the Matcher contains information about matches found (or not found) in the input string. You can access that information by calling various methods on Matcher:

  • matches() tells you if the entire input sequence was an exact match for the pattern.
  • start() tells you the index value in the string where the matched string starts.
  • end() tells you the index value in the string where the matched string ends, plus one.

Listing 21 finds a single match starting at 0 and ending at 7. Thus, the call to matches() returns true, the call to start() returns 0, and the call to end() returns 8.

lookingAt() versus matches()

If your string had more elements than the number of characters in the pattern you searched for, you could use lookingAt() instead of matches(). The lookingAt() method searches for substring matches for a specified pattern. For example, consider the following string:
a string with more than just the pattern.
If you search this string for a.*string, you get a match if you use lookingAt(). But if you use matches(), it returns false, because there's more to the string than what's in the pattern.

Complex patterns in regex

Simple searches are easy with the regex classes, but you can also do highly sophisticated things with the Regular Expressions API.
Wikis are based almost entirely on regular expressions. Wiki content is based on string input from users, which is parsed and formatted using regular expressions. Any user can create a link to another topic in a wiki by entering a wiki word, which is typically a series of concatenated words, each of which begins with an uppercase letter, like this:
MyWikiWord
Suppose a user inputs the following string:
Here is a WikiWord followed by AnotherWikiWord, then YetAnotherWikiWord.
You could search for wiki words in this string with a regex pattern like this:
[A-Z][a-z]*([A-Z][a-z]*)+
And here's code to search for wiki words:
String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";
Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
  Logger.getAnonymousLogger().info("Found this wiki word: " + matcher.group());
}
Run this code, and you can see the three wiki words in your console.

Replacing strings

Searching for matches is useful, but you also can manipulate strings after you find a match for them. You can do that by replacing matched strings with something else, just as you might search for text in a word-processing program and replace it with other text. Matcher has a couple of methods for replacing string elements:

  • replaceAll() replaces all matches with a specified string.
  • replaceFirst() replaces only the first match with a specified string.

Using Matcher's replace methods is straightforward:
String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";
Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
Matcher matcher = pattern.matcher(input);
Logger.getAnonymousLogger().info("Before: " + input);
String result = matcher.replaceAll("replacement");
Logger.getAnonymousLogger().info("After: " + result);
This code finds wiki words, as before. When the Matcher finds a match, it replaces the wiki word text with its replacement. When you run the code, you can see the following on your console:
Before: Here is WikiWord followed by AnotherWikiWord, then SomeWikiWord.
After: Here is replacement followed by replacement, then replacement.
If you had used replaceFirst(), you would have seen this:
Before: Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.
After: Here is a replacement followed by AnotherWikiWord, then SomeWikiWord.

Matching and manipulating groups

When you look for matches against a regex design, you can get data about what you found. You've seen some of that ability with the begin() and end() strategies on Matcher. But at the same time it's conceivable to reference coordinates by catching gatherings. 
In each example, you regularly make bunches by encasing parts of the example in brackets. Gatherings are numbered from left to right, beginning with 1 (amass 0 speaks to the whole match). The code in Listing 22 replaces each wiki word with a string that "wraps" the word.
Listing 22. Matching groups
String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";
Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
Matcher matcher = pattern.matcher(input);
Logger.getAnonymousLogger().info("Before: " + input);
String result = matcher.replaceAll("blah$0blah");
Logger.getAnonymousLogger().info("After: " + result);
Run the Listing 22 code, and you get the following console output:
Before: Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.
After: Here is a blahWikiWordblah followed by blahAnotherWikiWordblah,
  then blahSomeWikiWordblah.
Listing 22 references the entire match by including $0 in the replacement string. Any portion of a replacement string of the form $int refers to the group identified by the integer (so $1 refers to group 1, and so on). In other words, $0 is equivalent to matcher.group(0);.
You could accomplish the same replacement goal by using other methods. Rather than calling replaceAll(), you could do this:
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
  matcher.appendReplacement(buffer, "blah$0blah");
}
matcher.appendTail(buffer);
Logger.getAnonymousLogger().info("After: " + buffer.toString());
And you'd get the same result:
Before: Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.
After: Here is a blahWikiWordblah followed by blahAnotherWikiWordblah,
  then blahSomeWikiWordblah.