Regular Expression, Regular Expression

Source: Internet
Author: User
Tags character classes

Regular Expression, Regular Expression

Regular Expression:It refers to a single string used to describe or match a series of strings that conform to a certain syntax rule. It is actually a rule with its own special applications.

Commonly used:

Character class:

X indicates that the Escape Character "\" \ t (Tab) is composed of one character) represents a "\ t" symbol \ n represents the line feed symbol [abc] a or B or c [^ abc] any character except a, B, c [a-z] Any character in a-z, lowercase letter; any character in [a-zA-Z] a-zA-Z. case-insensitive [0-9] indicates any number Predefined character classes:. Any one character \ d Number: [0-9] \ D non-digit: [^ 0-9] \ w word character: [a-zA-Z_0-9] \ W non-word character: [^ \ w] \ s indicates any blank character. For example, \ t \ n \ S indicates any non-blank character. Border matching (mainly used in js ):^ The beginning of a row $ the end of a row \ B word boundary example: "I am here ". matches ("I \ B \ bam \ B \ bhere") Note: The position of the boundary. Greedy -- quantity expressionX? Is there no regular expression once or once? Indicates that the regular expression appears once or once. X, zero, one or multiple times X + X, one or more times X {n} X, EXACTLY n times X {n, m} X, at least n times, but not more than m times
Sample Code:
1 package com. ibeifeng. regex; 2 public class RegexDemo2 {3 public static void main (String [] args) {4/* 5 * [abc] a or B or c 6 * [^ abc] any character other than a, B, and c 7 * [a-zA-Z] -Any character in zA-Z is 8 */9 System. out. println ("d ". matches ("[abc]"); 10 System. out. println ("". matches ("[^ abc]"); 11 System. out. println ("aa ". matches ("[a-zA-Z]"); 12 13/* 14 * \ d15 * "\" represents a "\" 16 * \ w 0-9a-zA-Z_17 */18 System. out. println ("". matche S ("\ d"); 19 System. out. println ("~ ". Matches ("\ D"); 20 System. out. println ("0 ". matches ("\ w"); 21 22/* 23 * ^ 24*$25 * \ b26 */27 System. out. println ("acbc ". matches ("^ a \ w"); 28 System. out. println ("abcde ". matches ("\ w \ we $"); 29 30/* 31 *? 1 time or 0 times 32 ** 0 times or multiple times 33 * + 1 time or multiple times 34 * {n} Exactly N times 35 * {m, n} At least m times up to n times 36 */37 System. out. println ("carsdff ". matches ("\ w *"); 38 System. out. println ("teacher ". matches ("\ w +"); 39 System. out. println ("abcq ". matches ("\ w {3}"); 40 System. out. println ("=========================="); 41 System. out. println ("I am here ". matches ("I \ B \ bam \ B \ bhere"); 42 System. out. println ("". matches (". "); 43} 44}

Sample Code (/Email Detection):

1 package com. ibeifeng. regex; 2 public class RegexDemo3 {3 public static void main (String [] args) {4 // match the regular expression 5 String s1 = "abcde@163.com "; 6 String s2 = "abcdefg@qq.com"; 7 String s3 = "adfdfw@sina.com.cn"; 8 String s4 = "fwef # com.cn.cn.cn"; 9/* 10 *. \. 11 * @ \ @ 12 */13 System. out. println (s4.matches ("\ w + \\@\\ w + (\\. \ w {2, 3}) {1, 2} "); 14} 15}


Split function:

Example:Try entering an age to show if it is within the range of the appropriate age tip size

1 package com. ibeifeng. regex; 2 import java. util. arrays; 3 import java. util. required; 4/* 5 * public String [] split (String regex) 6 */7 public class RegexDemo5 {8 public static void main (String [] args) {9 // String age = "18-28"; 10 // try to enter an age that shows whether the appropriate age is in the range. The prompt size is 11 String age = "18-28 "; 12 String [] split = age. split ("-"); 13 System. out. println (Arrays. toString (split); 14 int min = Integer. valueOf (split [0]); 15 int max = Integer. valueOf (split [1]); 16 bytes = new bytes (System. in); 17 System. out. print ("Enter the target age:"); 18 int nextInt = bytes. nextInt (); 19 if (nextInt> max) {20 System. out. println ("Haha"); 21} else if (nextInt <min) {22 System. out. println ("children"); 23} else {24 System. out. println ("here is what you want. "); 25} 26} 27}

Replacement function:

1 package com. ibeifeng. regex; 2 import java. util. arrays; 3 public class RegexDemo6 {4 public static void main (String [] args) {5 6 7/* 8 * public String replaceAll (String regex, 9 * String replacement) 10 */11 // fuck f *** shit s *** cao c *** 12 String str5 = "Oh shit! Cao! What a fucking weather, what are you fucking doing! "; 13 String replaceAll = str5.replaceAll (" fuck "," f *** ") 14. replaceAll ("shit", "s ***") 15. replaceAll ("cao", "c **"); 16 17 System. out. println (replaceAll); 18 19 String str6 = "show me the money 999999"; 20 // Replace the same number with * 21 System. out. println (str6.replaceAll ("\ d +", "*"); 22 System. out. println (str6.replaceAll ("\ d {3}", "*"); 23} 24}

Function:

The Pattern and Matcher classes use the Pattern class. If you want to obtain such objects, you must use the compile () method. The function of the method is to compile regular expressions. The Matcher class is obtained through the Pattern class. Step: 1. Obtain Patterm by compiling rules; 2. Obtain the matching string by matching the Character Sequence; 3. Obtain find by matching and obtain it in the group. Pattern pattern = Pattern. compile (String regex); Matcher matcher = pattern. matcher (String str); while (matcher. find () {String str = matcher. group ();}
1 package com. ibeifeng. regex; 2 import java. util. regex. matcher; 3 import java. util. regex. pattern; 4 public class PatternDemo {5 public static void main (String [] args) {6 // get the three-character word 7 // You may be out of my sight, but never out of my mind. 8 String s = "You may be out of my sight, but never out of my mind"; 9 // 1. create Pattern10 Pattern pattern = Pattern based on the regular expression. compile ("\ B \ w {3} \ B"); 11 // 2. obtain the Matcher = pattern from the pattern matching character sequence. matcher (s); 13 // 3. obtain 14 int num = 0; 15 while (matcher. find () {16 String group = matcher. group (); 17 int start = matcher. start (); 18 int end = matcher. end (); 19 System. out. println (group); 20 System. out. println (start + "=" + end); 21 num ++; 22} 23 System. out. println (num + "times"); 24 25/* matcher. find (); 26 String group = matcher. group (); 27 System. out. println (group); 28 29 matcher. find (); 30 group = matcher. group (); 31 System. out. println (group); */32} 33}

 

 

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.