Regular expressions are a powerful and flexible text-processing tool. Using it, we can construct complex text patterns in a programmatic way, and search for input strings. Once you've found the parts that match these patterns, you'll be able to handle them as you please.
About the regular expression of the syntax, the article on the internet is a lot of articles, really can not find, but also to view the Java API documentation, not much introduction. Here is an introduction to a gadget that can test regular expressions. Directly on the code:
1 PackageCom.test.stringregex;2 //{ARGS:ABCABCABCDEFABC "abc+" "(ABC) +" "(ABC) {2,}"}3 ImportJava.util.regex.Matcher;4 ImportJava.util.regex.Pattern;5 6 Public classtestregularexpression {7 Public Static voidMain (string[] args) {8 if(Args.length < 2) {9System.out.println ("Usage:\njava testregularexpression" +Ten"Charactersequence regularexpression+"); OneSystem.exit (0); A } -System.out.println ("Input: \" "+ args[0] +" \ ""); - for(String Arg:args) { theSystem.out.println ("Regular expression: \" "+ arg +" \ ""); -Pattern p =Pattern.compile (ARG); -Matcher m = P.matcher (args[0]); - while(M.find ()) { +System.out.println ("Match \" "+ m.group () +" \ "at positions" + -M.start () + "-" + (M.end ()-1)); + } A } at } -}
View Code
The parameters for the input test are {ARGS:ABCABCABCDEFABC "abc+" (ABC) + "" (ABC) {2,} "in the comment, the result is as follows
The first console parameter is to be used to search for a matching input string, followed by one or more parameters are regular expressions, in "", here I tested 3 regular expressions "abc+" (ABC) + "" (ABC) {2,} ", The actual use of the process can be replaced with the expression you want to test, to verify that they have the matching function you need.
The pattern object represents the compiled regular expression, the Matcher () method on the compiled pattern object, and an input string that together constructs a matcher () object. By calling the Matcher () object's Find (), group (), Start (), The End () method forms the visual output of the tool class, whether the regular expression matches, and how to match the target string (that is, the parameter args (0)) at a glance.
Java Regular Expression--a gadget for testing regular expressions