The code fragment above specifies a regex that matches a comma character immediately followed by a single-space character and produces the following output:
John Doe
47
Hillsboro Road
32000
Note |
String incorporates three convenience methods that invoke their equivalent Pattern methods: public boolean matches(String regex) , public String [] split(String regex) , and public String [] split(String regex, int limit) . |
Matcher methods
Matcher
objects support different kinds of pattern match operations, such as scanning text for the next match; attempting to match the entire text against a pattern; and attempting to match a portion of text against a pattern. Accomplish those match operations with the following methods: - public boolean find()
:
scans text for the next match. That method either starts its scan at the beginning of the text or, if a previous invocation of the method returned true and the matcher has not been reset, at the first character following the previous match. A Boolean true value returns if a match is found. Listing 1 presents an example. - public boolean find(int start)
:
resets the matcher and scans text for the next match. The scan begins at the index specified bystart
. A Boolean true value returns if a match is found. Example:m.find (1);
scans text beginning at index 1. (Index 0 is ignored.) Ifstart
contains a negative value or a value exceeding the length of the matcher's text, this method throws anIndexOutOfBoundsException
object. - public boolean matches()
:
attempts to match the entire text against the pattern. That method returns a Boolean true value if the entire text matches. Example:Pattern p = Pattern.compile ("\\w*"); Matcher m = p.matcher ("abc!"); System.out.println (p.matches ());
outputsfalse
because the entireabc!
text lacks word characters. - public boolean lookingAt()
:
attempts to match the text against the pattern. That method returns a Boolean true value if the text matches. Unlikematches()
, the entire text does not need to be matched. Example:Pattern p = Pattern.compile ("\\w*"); Matcher m = p.matcher ("abc!"); System.out.println (p.lookingAt ());
outputstrue
because the beginning of theabc!
text consists of word characters only.
No comments:
Post a Comment