Java Regular Expression--a gadget for testing regular expressions

Source: Internet
Author: User

  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.