In the common Java programming, regular expressions are often combined with the string processing, Io flow, in the "Java programming thought" even the regular expression in the description of the IO stream in the content of the chapter, it can be seen that the regular expression is closely related to the flow. Java designers also use the package that the regular expression requires: Java.util.regex is placed in a common tool.
In Java,
the quantifier of the regular expression is divided into three levels:
1) the greedy
To make as many matches as possible for all possible patterns, we typically use this pattern;
2) barely.
As long as the match to no longer proceed, this pattern in the string of multiple self-contained methods have a good embodiment;
3) accounted for
This pattern is only available in Java, and is used to place regular expressions out of control, which we don't normally use.
Implementation method:
We use the pattern and Matcher classes in Java.util.regex to achieve generic regular expression matching.
A pattern object represents a compiled version of a regular expression. The static Complie () method compiles a regular expression into a pattern object, and through a pattern instance of the Matcher () we can get a Matcher object instance that can present the result we want;
Cases:
Pattern p = pattern.compile ("a*b"= P.matcher ("Aaaaab"); Boolean B = m.matches ();
This is the most typical example of a regular expression, "Aaaaab" is the input, "a*b" is the matching pattern, if the input can be matched by a pattern, the value of B is true, and vice versa is false.
Pattern has two very useful methods, one is static public static Boolean matches (string regex, charsequence input), which is used to view the string in input that has not been matched by a regex;
Another is the split () method, which produces an array that is broken by the regex, much like the method of string. What is Charsequence
There are many useful methods in matcher, such as the Find () method, you can find multiple pattern matches and return a Boolean value, so it is generally used in the while loop.
Example: How to use regular expressions to extract URLs from a Web page? This is used more in web crawlers.