Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Tuesday November 11, 2008
11:29 AM

Java gravel of the day: regular expressions

[ #37856 ]

I knew that Java's regular expressions would come with a horrible syntax I wouldn't want to use. I didn't know that the syntax would appear to be designed by someone who completely misses the point.

In order to use a precompiled regex pattern, you get it to produce a Matcher object ... which is constructed with the String you want to match against! Then you call matches() on the Matcher object.

Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();

For what mind numbing purpose was it decided that a Matcher object should have a String to match against as a piece of instance data? Are you going to construct one of these Matchers and pass it around so that different pieces of code can check over and over again if the same String matches?

This could have simply been implemented as Pattern.matches(String), or, if you simply must have a Matcher class, then get your Matcher from Pattern.matcher(), taking no parameters, and then require a String parameter to Matcher.matches(String).

What possible reason could there be for not following either of those approaches, instead of what they gave me?

As a sop, you get a static Pattern.matches(String pattern, String) method, but that completely prevents you from using precompiled patterns.

Clueless, Sun. Just clueless. Or is it the Java community that I have to blame for this? I've seen so many features elegantly added into Java, and then I see stuff like this.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • First of all there is a convenience method of matches for a Pattern.

    boolean b = Pattern.matches("a*b", "aaaaaab");

    That suffices for the common case. (Too bad they didn't think of allowing matches to pass in flags to compile with.)

    Java allows you to reuse the same pattern over and over again. It is more verbose than it would be if they had a utility function of matches on a Pattern, but the functionality is there. Plus there is always the possibility that Java will be smart enough at some point to automat

    • That does seem to show a use case for the Matcher object. For the record, I almost never loop over a match in Perl. I just don't seem to find it necessary. I know why you'd want to, though, so Java should make that possible.

      Interestingly enough, that seems to be the only explictly java.util.regex-related way to accomplish a s///g, as well, although there's a regex-related method in the String class that will do it.

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • Hi Folks

    Java is designed to cripple your hardware. It has no other purpose.

    Exactly the same goes for each any every piece of MS software.