Java Regular Expressions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 44: Line 44:


{{External|https://github.com/NovaOrdis/playground/tree/master/java/regex/simplest}}
{{External|https://github.com/NovaOrdis/playground/tree/master/java/regex/simplest}}
==The Difference between matches() and find()==


=java.langString API=
=java.langString API=

Revision as of 21:50, 29 July 2017

External

Internal

Overview

Regular expressions can be used in Java via the String API or java.util.regex API.

java.util.regex API

The default sequence for using regular expressions consists in building a Pattern instance, which then can be matched against multiple strings by applying it via Matcher instances. The Pattern instance contains a compiled representation of the regular expression. The Matcher uses the Pattern, but encapsulates all the state required to perform matching against a String, so the Pattern can be shared by multiple Matchers. The Matcher instances are not thread safe, see Concurrent Usage Considerations below.

public class Example {

  public static final Pattern PATTERN = Pattern.compile(".*red.*");

  ...

  public void useRegex(String argument) {

      Matcher m = PATTRN.matcher(argument);

      ...

  }

Once built, a Matcher instance can be used to match or find.

Matcher.matches()

The Matcher.matches() method attempts to match the entire input sequence against the pattern.

Matcher.find()

Working code is available here:

https://github.com/NovaOrdis/playground/tree/master/java/regex/simplest

java.langString API

String s = "...";
s.matches(...);

While convenient in some cases, the String API also delegates to the java.util.regex API via the Pattern.matches() call. This method is not efficient when used repeatedly, because it internally builds a Pattern instance on each invocation. If matching against the same regular expression is to be done repeatedly, java.util.regex API is preferred.

Concurrent Usage Considerations


Matcher instances are NOT thread safe, create a matcher per thread

Regular Expression Syntax