Regular expression (. *?)

From the Internet, leaving to seriously look again

1 ,. matches any character except newline "\ n" outside;
2, * indicates a character zero or infinity times before the match;
3, * + or heel? It represents a non-greedy match, that match as little as possible, such as *? Repeated any number of times, but less duplication wherever possible;
.? 4, * denotes any number of matching repeat, but with minimal repeats make the whole premise of a successful match. Such as: a * b matching the shortest start with a to b the end of the string.?. If you apply it to aabab, then it will match aab and ab.

 

The following is a detailed explanation of the main

1. *
Indicates match except newline \ any single character other than n, * represents zero or more times. So * together, they appear to represent any character zero or more times. No? Represents the greedy mode. For example a. * B, which will be the longest match to a start to end of the string b. If you use it to search aabab, then it will match the entire string aabab. This is known as greedy matching.
Another example mode src = `. *`, It will match the longest to start src = `,` the longest string ending. When using it to search <img src = `` test.jpg` width = `60px` height =` 80px` />, will return src = `` test.jpg` width = ` 60px` height =` 80px`

2. *?
? * Or + followed when behind, showing a lazy mode. Also known as non-greedy mode. That is, matching as few characters. Means match any number of repetition, but with minimal repeats make the whole premise of a successful match.
a. *? b matching the shortest start with a to b the end of the string. If you apply it to aabab, then it will match aab (first to third character) and ab (fourth to fifth character).
Another example mode src = `. *?`, It will match src = `beginning to the end of the` shortest possible string. And the start and end of an intermediate character can not, because * means zero or more. When using it to search <img src = `` test.jpg` width = `60px` height =` 80px` />, will return src = ``.

3. +?
Ibid.,? * Or + followed when behind, showing a lazy mode. Also known as non-greedy mode. Means match any number of repetition, but with minimal repeats make the whole premise of a successful match.
a. +? b matching the shortest start with a, b end of the string, but the middle of a and b must have at least one character. If you apply it to ababccaab, then it will match abab (first to fourth character) and aab (seventh to ninth character). Note that at this time the result is not a match ab, ab and aab. Since the intermediate a and b must be at least one character.
Another example mode src = `. +?`, It will match src = `beginning to the end of the` shortest possible string. And the beginning and end of the middle must have character, because + 1 to represent more. When using it to search <img src = `` test.jpg` width = `60px` height =` 80px` />, will return src = `` test.jpg`. Note that the. *? Difference at this time does not match src = ``, because at least one character between src = `and`.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;

public class TestRegx {

  @Test
  public void testRegx(){

    String str = "<img src=``test.jpg` width=`60px` height=`80px`/>";

    String pattern1 = "src=`.*`";
    String pattern2 = "src=`.*?`";
    String pattern3 = "src=`.+?`";

    Pattern p1 = Pattern.compile(pattern1);
    Pattern p2 = Pattern.compile(pattern2);
    Pattern p3 = Pattern.compile(pattern3);

    Matcher m1 = p1.matcher(str);
    Matcher m2 = p2.matcher(str);
    Matcher m3 = p3.matcher(str);

    System.out.println("根据pattern1匹配的结果:");
    if (m1.find()) {
      for(int i=0; i<=m1.groupCount(); i++){
        System.out.println(m1.group(i));
      }
    }

    System.out.println ( "The result of the matching pattern2: " );
     IF (m2.find ()) {
       for ( int I = 0 ; I <= m2.groupCount (); I ++ ) { 
        . The System OUT .println (m2.group (I)) ; 
      } 
    } 

    the System. OUT .println ( " the matching result pattern3: " );
     IF (m3.find ()) {
       for ( int I = 0 ; I <= m3.groupCount (); I ++ ) { 
        the System. OUT .println (m3.group (I)); 
      } 
    } 

    String [] str1 = p1.split (STR); 
    String [] str2 = p2.split (STR); 
    String [] Str3 = p3.split (STR); 

    . The System OUT .println ( " The result pattern1 sliced " ) ;
     for ( int I = 0 ; I <str1.length; I ++ ) { 
      the System. OUT .println (str1 [I]); 
    } 

    the System. OUT .println ( " according to the results of the segmentation pattern2 " );
     for ( int I = 0 ; I <str2.length; I ++ ) { 
      the System. OUT.println(str2[i]);
    }

    System.out.println("根据pattern3切分的结果");
    for (int i=0; i< str3.length; i++) {
      System.out.println(str3[i]);
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/wjx-blog/p/12123279.html