Java uses regular expressions

Source: Internet
Author: User
Tags expression engine

 PackageCom.regexp;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classTest { Public Static voidMain (string[] args) {//Regular Expressions//a "." represents an arbitrary characterP ("ABC". Matches ("...")); //"\d" represents a number, \d equivalent to not a number, the \d is reversedP ("As897as98fsg6dfg9sddf". ReplaceAll ("\\d", "_")); //Pattern can compile a schema for regular expressionsPattern p = pattern.compile ("[A-z]{3}"); Matcher m= P.matcher ("Erf"); P (m.matches ());//The matches () method is always matched to the entire string//the above three lines of code is equivalent to the following sentence, but the above three sentences performance is relatively high//{n}, must appear n times//{n,}, at least n times, up to No limit//{n,m}, appears at least n times, no more than m timesP ("QQQ". Matches ("[A-z]{3}"))); //preliminary understanding of ".", "*", "+", "?" Matadata (source data)P ("a". Matches ("."));//an arbitrary characterP ("AA". Matches ("AA"))); P ("AAAA". Matches ("A *"));//0 or moreP ("AAAA". Matches ("A +"));//1 or moreP ("AAAA". Matches ("A?"));//1 or not presentP ("". Matches ("A?"));//0 Width MatchingP ("a". Matches ("a?"))); P ("45913549872168435". Matches ("\\d{3,10}")); P ("192.168.0.222". Matches ("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}")); P ("192". Matches ("[0-2][0-9][0-9]")); //RangeP ("a". Matches ("[ABC]"));//[] refers to matching a character, no matter how long it is writtenP ("L". Matches ("[^ABC]"));//"^" other than ABC can matchP ("a". Matches ("[A-za-z]"));//"-" indicates from which range to which range (this means lowercase or uppercase letters)P ("a". Matches ("[a-z]|[ A-z] "));//Ibid .P ("a". Matches ("[a-z[a-z]]"));//Ibid .P ("R". Matches ("[A-Z&&[RFG]]");//colleagues meet two scopes//meet \d \s \\u \P ("\n\t\r". Matches ("\\s{4}"));//"\s" is a blank character, such as a space, line break, tab, etc.P ("FA". Matches ("\\s{1,8}"));//" \s" is the inverse of "\s "P ("A_8". Matches ("\\w{3}"));//"\w" refers to a character that can be used as a name: uppercase and lowercase underscores + numbersP ("abc807$%". Matches ("[a-z]{1,3}\\d+[&^%$]+"))); P ("\ \". Matches ("\\\\"))); //Boundary Matching DeviceP ("Hello sir". Matches ("^h.*"));//"^" represents the beginning of a line when it is outside "[]" .P ("Hello sir". Matches (". *ir$"));//"$" is put at the end of the end of the line represented byP ("Hello#sir". Matches ("^h[a-z]{3}o\\b.*"));//the "\b" word boundary can include those whitespace characters or "\w", etc.P ("Hellosir". Matches ("^h[a-z]{3}o\\b.*"))); //Filter Blank Lines ("*" is used to decorate "^[\\s&&[^\\n]")P ("\ t \ n". Matches ("^[\\s&&[^\\n]]*\\n$"))); //Email AddressP ("[email protected]". Matches ("[\\w[.-]][email protected][\\w[.-]]+.[ \\w]+ ")); //The Matches.find () method is to find substrings in a string that conform to a regular expression//Regular Expression search engines need to be aware of a pointPattern P1 = Pattern.compile ("\\d{3,5}"); String s= "123-4567-8912-12345-ac"; P ("Total length of string:" +s.length ()); Matcher M1=P1.matcher (s);        P (m1.matches ()); //when the previous line of code is executed, the regular expression engine has already matched string s, and several subsequent find () methods are from "4567 ..." Started looking.//This means that the find () method and the matches () are mutually affectedM1.reset ();//call the Reset () method to resolve the mutual effects of find and matchesP (M1.find ()); P (M1.start ()+"----------"+m1.end ());        P (M1.find ()); P (M1.start ()+"----------"+m1.end ());        P (M1.find ()); P (M1.start ()+"----------"+m1.end ());        P (M1.find ()); P (M1.start ()+"----------"+m1.end ());        P (M1.find ()); //If you want to output the location of the substring found, if you must be able to find, otherwise execute the following line of code will be an error!P (M1.start () + "----------" +m1.end ()); //The Lookingat () method is to find the entire string from the start positionP (M1.lookingat ());        P (M1.lookingat ());        P (M1.lookingat ());                P (M1.lookingat ()); //simply do an exercise to find out all the URLsString News = "The People's livelihood in the diligent, diligent is not the Chamber www.baidu.com--Xi greatly used more than 2000 years ago," left biography "in the old adage www.sina.com.cn interpretation of the most simple truth. "+" 2013spring.io April 28, "+" Xi greatly in the National Federation of Trade Unions and the national www.qq.com model worker representatives, stressed that "+" must be firmly established Www.hanqi.net Labor is the most glorious, the highest work, the greatest labor, the most beautiful concept of labor, advocating labor, the benefit of workers, "+" let the whole people further glow of labor enthusiasm, release to create news.ifeng.com potential, through labor to create a better life. ";                SYSTEM.OUT.PRINTLN (news); Pattern P2= Pattern.compile ("[[a-z][.]] +[\\w]+ "); Matcher m2=P2.matcher (News);  while(M2.find ()) {//p (news.substring (M2.start (), M2.end ()));P (M2.group ()); }                //Substitution of strings//Pattern.case_insensitive (a constant in Pattern, ignoring case)Pattern p3 = pattern.compile ("java", pattern.case_insensitive); String S1= "Java Java java java I like Java qwert"; Matcher M3=P3.matcher (S1); intB=0; StringBuffer buf=NewStringBuffer ();  while(M3.find ()) {//ReplaceAll directly returns the entire string that has been replaced//p (M.replaceall ("Java"));b++; //If you want to convert an even bit of Java to lowercase, the odd digits are replaced with uppercase            if(b%2==0) {m3.appendreplacement (buf,"Java"); } Else{m3.appendreplacement (buf,"JAVA");        }} m.appendtail (BUF);            P (buf.tostring ()); //Grouping of regular Expressions//application: In the following example, if you want to take out each of the matching strings, you also want to take out a string that is just a number//You can use grouping, grouping rules, from left to right number of "(", that is, the first few groupsPattern P4 = Pattern.compile ("((\\d{3,5}) ([a-z]{2})]"); String S2= "123av-4567aa-8912qq-12345vv-ac"; Matcher M4=p4.matcher (S2);  while(M4.find ()) {p (M4.group (3)); }    }         Public Static voidp (Object o) {System.out.println (o); }}

Java uses 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.