[ajug-members] Java vs sed Regular Expression

Robert Watkins rwatkins at foo-bar.org
Sun Aug 12 12:56:36 EDT 2007


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
--------------------



More information about the ajug-members mailing list