Java Regular Expressions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 14: Line 14:
=java.util.regex API=
=java.util.regex API=


The common approach for using regular expressions consists in building a [http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html Pattern] instance, which can be matched against multiple strings by applying it via [http://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html Matcher] instances. The Matcher instances are not thread safe, see [[]].
The common approach for using regular expressions consists in building a [http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html Pattern] instance, which can be matched against multiple strings by applying it via [http://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html Matcher] instances. The Matcher instances are not thread safe, see [[#Concurrent_Usage_Considerations|Concurrent Usage Consideratins]] below.


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>

Revision as of 21:29, 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 common approach for using regular expressions consists in building a Pattern instance, which can be matched against multiple strings by applying it via Matcher instances. The Matcher instances are not thread safe, see Concurrent Usage Consideratins below.

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.

Concurrent Usage Considerations


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

Regular Expression Syntax