First, a demo program
Java's regular expression package is Java.util.regex, mainly using the pattern and matcher.
The GroupCount method is callable, and most methods must be called after the match attempt succeeds.
The main methods are:
Boolean find ()
The Boolean value returned indicates whether a match can be found, and if there are multiple calls, try a new match each time after the last matching position.
public class Regex {public static void Main (string[] args) { String regex = "\\w+"; Pattern pattern = pattern.compile (regex); String str = "This is my"; Matcher Matcher = Pattern.matcher (str); while (Matcher.find ()) { String matchedtext = Matcher.group (); int matchedfrom = Matcher.start (); int matchedto = Matcher.end (); System.out.println (matchedfrom+ "" +matchedto); System.out.println (Matchedtext); }
Boolean Find (int offset)
If an integer parameter is specified, the match attempt starts at the position of the offset character at the beginning of the target string, and if an error is exceeded, this form of find is not affected by the current search scope.
Boolean matches ()
Returns whether the regular expression can exactly match the text of the range retrieved in the target string.
Boolean Lookingat ()
Returns whether the regular expression can find a match in the current retrieval scope of the current target string.
Group ()
Returns the matching text of the previous applied regular expression.
GroupCount ()
Returns the number of captured brackets
Group (int num)
Returns the corresponding capturing brace match, group (0) equals group ()
The outputs are:
This is
2
This
Is
int start (int num)
Returns the absolute offset value in the target string of the starting point of the matched text of the captured bracket numbered Num.
int start ()
Returns the absolute low value of the entire match starting point, which is equivalent to start (0)
int end with int end (int num) corresponds to start
Regex reading Notes (V) Java Operations Chapter