Week 95 — How can one find all ways a `String` matches a given regex?
Question of the Week #95
How can one find all sways a
String
matches a given regex?1 Reply
The
Pattern
class can be used to for regex matching.
To create a Pattern
from a String
containing the regex, the Pattern.compile
method can be used.
With a Pattern
, it's possible to obtain a Matcher
matching the pattern against a given String
. The matches()
method of that Matcher
can then be used to check whether the String
matches the given regex:
Instead of matching the whole String
, it's also possible to find matches of the pattern within a given String
using Matcher#find
. This method returns true
if a match has been found.
The above code prints the following:
📖 Sample answer from dan1st