"Crazy Java Handout" (22)----Regular Expressions

Source: Internet
Author: User

Pattern p = pattern.compile ("a*b"= P.matcher ("Aaaab"); Boolean // B = True

Pattern objects can be reused multiple times. If a regular expression needs to be used only once, you can use the static matches method of the pattern class directly:

Boolean b = pattern.matches ("A*b", "Aaaab");

Pattern is an immutable class that can be used safely by multiple concurrent threads.

Matcher class Examples:

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classFindgroupdemo { Public Static voidMain (string[] args) {Matcher m= Pattern.compile ("\\w+"). Matcher ("Java is very easy");  while(M.find ()) {System.out.println (M.group ()); }        inti = 0;  while(M.find (i)) {System.out.print (M.group ()+ "\ T"); I++; }    }}

The Find () method finds substrings in a string that match the pattern one time, and once the corresponding substring is found, the next time the Find () method is called, it is then looked down. In addition, find () can also wear parameters of type int, and the Find () method searches down from the int index.

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classStartenddemo { Public Static voidMain (string[] args) {String regstr= "Java is very easy!"; System.out.println ("Target string:" +regstr); Matcher m= Pattern.compile ("\\w+"). Matcher (REGSTR);  while(M.find ()) {System.out.println (M.group () )+ "Start:" + m.start () + ", End:" +m.end ()); }    }}

The start and end methods are primarily used to determine the position of a substring in a target string

"Crazy Java Handout" (22)----Regular Expressions

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.