After creating a
Pattern
object, you commonly obtain a Matcher
object from Pattern
by calling Pattern
's public matcher(CharSequence text)
method. That method requires a single text
object argument, whose class implements the CharSequence
interface. The obtained matcher scans the characters in the text
object during a pattern match operation. Example: Pattern p = Pattern.compile ("[^aeiouy]"); Matcher m = p.matcher ("This is a test.");
obtains a matcher to match nonvowel characters in This is a test.
. Creating
Pattern
and Matcher
objects is bothersome when you wish to quickly check if a pattern completely matches a text sequence. Fortunately, Pattern
offers a convenience method to help you accomplish that task: public static boolean matches(String regex, CharSequence text)
. That static method returns a Boolean true value if and only if the entire text
character sequence matches regex
's pattern. Example: System.out.println (Pattern.matches ("[a-z[\\s]]*", "all lowercase letters and whitespace only"));
returns a Boolean true value, indicating only whitespace characters and lowercase letters appear in all lowercase letters and whitespace only
.
No comments:
Post a Comment