[ajug-members] Java vs sed Regular Expression

Sony Antony sony.antony at gmail.com
Sun Aug 12 16:21:48 EDT 2007


The difference/advantage of being able to precompile is the fact that
it only needs to be done once.
If you are doing a pattern matching on a huge file, and if the exact
same regular expression needs to be compiled for every line, then that
is a lot of wasted cpu cycles.

String.replaceAll() can be done by doing the precompile as I mentioned.
But I couldnt find an similar way for String.matches()

--sony



On 8/12/07, Robert Watkins <rwatkins at foo-bar.org> wrote:
> On Sun, 12 Aug 2007, Sony Antony wrote:
>
> > Has anybody compared the Regular Expression speed of Java against the
> > typical UNIX utilities like sed/awk/grep.
> >
> No idea. I would have thought, however, that different contexts would
> call for different applications: on the command line I would default to
> sed, but in a Java application I would stay inside Java. My guess is
> that sed would be faster, but of course it depends on what you're
> passing to it and ... in what context.
>
> > Also is there anyway to do a precompiled version of str.matches()
> > ( As in Pattern.compile( regex ) )
> >
> In a sense you've answered your own question. More explicitly, here is
> an approach, culled (and edited) from the Java API javadocs:
>
> An invocation of
>
>        boolean b = str.matches(regex)
>
> yields exactly the same result as the expression
>
>     boolean b = Pattern.matches(regex, str);
>
> So to precompile the pattern, do:
>
>        Pattern p = Pattern.compile(regex);
>
> then:
>
>        boolean b = p.matcher(str).matches();
>
> -- Robert
>
> --------------------
> Robert Watkins
> rwatkins at foo-bar.org
> --------------------
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>



More information about the ajug-members mailing list