Java-regular, java-

Source: Internet
Author: User

Java-regular, java-
Regular Expressions are common. But it is also very error-prone. Therefore, the methods for writing Java regular expressions are sorted out. Pattern ). Split (split _ into an array ). Compile (Compilation), matcher (matcher). + indicates that any unempty, \. indicates that the translation starts with ". ^" and ends with "$? It indicates the lazy mode. If the first matching condition is met, it will stop *: 0 to infinity +: 1 to infinity \ d Number: [0-9] \ D non-digit: [^ 0-9] \ w numbers and letters [a-zA-Z0-9] \ W non-numbers and letters [^ a-zA-Z0-9]

  • In some languages, "\" means "inserting a backslash in a regular expression. "But in Java," \ "means" to insert a backslash of a regular expression,
  • The java regular expression should be "\ w + ". If you want to insert a backslash, you must use "\\\\".
  • Java only uses a backslash "\ n \ t" for line breaks and grid jumps ".
Example: Pattern pattern = Pattern. compile ("[, |] + ");
String [] strs = pattern. split ("Java Hello World Java, Hello, World | Sun ");
For (int I = 0; I <strs. length; I ++ ){
System. out. print ("\ t" + strs [I]);
} The output is: Java Hello World Sun verified email address: String str2 = "ceponline@yahoo.com.cn ";
Pattern pattern5 = Pattern. compile (". + @. + \... +? ");
Matcher matcher5 = pattern5.matcher (str2 );
System. out. print ("\ n" + matcher5.matches (); // return a Boolean value to remove the html Tag:
Pattern pattern6 = Pattern. compile ("<. +?> ");
Matcher matcher6 = pattern6.matcher ("<a href = \" index.html \ "> homepage </a> ");
String string = matcher6.replaceAll ("");
System. out. println ("\ r" + string );
// Intercept the url
Pattern pattern8 = Pattern. compile ("Matcher matcher8 = pattern8.matcher ("dsdsds If (matcher8.find ()){
System. out. println (matcher8.group (); // returns the matched string.
} Simple: example: String str = "Regular Expression Hello World, regular expression Hello World"; str. replace ("hello", "I want to replace hello") filter the number public Double regexGetMath (String matcher ){
Pattern pattern = Pattern. compile ("[^ 0-9/.]");
Matcher match = pattern. matcher (matcher );
String getStr = match. replaceAll ("");
Double getNum = Double. parseDouble (getStr );
Return getNum;
} Filter String public String regexGetLetterLow (String matcher ){
Pattern pattern = Pattern. compile ("[^ a-zA-Z]");
Matcher match = pattern. matcher (matcher );
String getStr = match. replaceAll (""). toLowerCase ();
Return getStr;
}

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.