Java 1.4 regex fun

I’m sure that everyone knows that Java 1.4 added regex capabilities through the java.util.regex package. I have existing applications using regex (from Jakarta Oro) that were built pre Java 1.4 so I haven’t looked at this support closely until now.

What I hadn’t realized was that you don’t need to use the Pattern or Matcher classes in java.util.regex to do simple regex substitutions, that capability is built right into java.lang.string!

An example of this usage would be formating phone numbers for storage or display. I usually like to store phone numbers as 10 character strings, and apply formating when they are displayed. This is easy using regex substitution. To format for storage:

String source = "(555) 555-5555";
String result = source.replaceAll("^1|\\D+", "");

The result will be "5555555555".

Conversely, to format for display:

String source = "5555555555;
String result = source.replaceAll("^(\\d{3})(\\d{3})(\\d{4})", 
				  "($1) $2-$3");

The result will be "(555) 555-5555".

The documentation for String.replaceAll is here and it is fairly straight forward:

public String replaceAll(String regex,
                         String replacement)

The first example used the regex: "^1|\\D+"  It says; match the first character if it is a "1" (^1) OR (|) any character that is not a number (\\D+) and replace with "" (the replacement).

The second example used the regex: "^(\\d{3})(\\d{3})(\\d{4})"  It says; match the first 3 characters and put them into a variable ($1) do the same for the next 3 ($2) and the final 4 ($3). Then using the replacement: "($1) $2-$3" wrap the contents of $1 with "()" add a space and the contents of $2, followed by a "-" and the contents of $3.

A good regex primer can be found here