General Use of Java Regular Expressions Pattern s and Matcher

Keywords: Programming Java

Method Description:

find() method is partial matching, returning true when partial matching and perfect matching, and false when matching is not. If the matched string has groups, the group() function can also be used to get the groups in the string.

matches() are all matches, it only returns true when matches are perfect, false when matches are not matched and false when matches are partial. e. If you want to verify whether an input data is all digital or other types, you usually use matches().

 

2. General Writing

Pattern pattern= Pattern.compile(".*?,(.*)");
Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
   return matcher.group(1);
}

 

3. Detailed explanation:

matches
public static boolean matches(String regex,  CharSequence input)

Compile the given regular expression and try to match the given input to it.   
Call the form of this convenient method
Pattern.matches(regex, input);
Pattern.compile(regex).matcher(input).matches() ; 
If you use a pattern many times, it is more efficient to reuse it after compiling it once than to call it every time.
Parameters:
regex - Expressions to be compiled
input - The sequence of characters to match
Throw out:
PatternSyntaxException - If the grammar of the expression is invalid

find
public boolean find() attempts to find the next subsequence of the input sequence that matches the pattern.   
This method starts at the beginning of the matcher region, and every call to find() continues to look backwards from the position where the last find() successfully matched until it reaches the end of the string. The first execution of find() will look up from the beginning of the string. If the first call to find() returns true and the second call returns false, then only one place in the string matches the given regular expression.   
If the match is successful, more information can be obtained through the start, end and group methods.   

matcher.start() returns the index position of the matched substring in the string.
matcher.end() returns the index position of the last character of the matched substring in the string.
matcher.group() returns the matched substring
Return:
Returns true if and only if the subsequence of the input sequence matches the pattern of the matcher.

 

Part IV. Examples of JAVA Regular Expressions

(1) Character matching

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(str); // String of operation
boolean b = m.matches(); //Returns a matching result
System.out.println(b); 

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(str); // String of operation
boolean b = m. lookingAt (); //Returns a matching result
System.out.println(b); 

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(str); // String of operation
boolean b = m..find (); //Returns a matching result
System.out.println(b); 


(2) Segmentation of strings

Pattern pattern = Pattern.compile(expression); //Regular expressions
String[] strs = pattern.split(str); //Operate the string to get the returned string array


(3) Replacement strings

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(text); // String of operation
String s = m.replaceAll(str); //Replaced strings



(4) Find and replace the specified string

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(text); // String of operation
StringBuffer sb = new StringBuffer(); 
int i = 0; 
while (m.find()) { 
    m.appendReplacement(sb, str); 
    i++;    //Number of occurrences of strings
} 
m.appendTail(sb);//Connect the following string from the interception point
String s = sb.toString(); 


Find the output string

Pattern p = Pattern.compile(expression); // Regular expressions
Matcher m = p.matcher(text); // String of operation
while (m.find()) { 
   matcher.start() ;
   matcher.end();
   matcher.group(1);
}

 

V. group Notes

The difference between group (), group (int i), and group count () requires understanding what capturing groups mean. The capture group is also a sub-Pattern in Patterns divided by ()

To illustrate with examples:

Pattern p = Pattern.compile("(ca)(t)");
Matcher m = p.matcher("one cat,two cats in the yard");
System.out.println("The number of matching groups obtained by this search is:" + m.groupCount()); // 2
for (int i = 0; i <= m.groupCount(); i++) {
	System.out.println("The first" + i + "The substring of a group is:" + m.group(i));
}

Output:

The number of matching groups obtained by this lookup is 2:
The substring of group 0 is: cat
The substring of group 1 is: ca
The substring of group 2 is:t

It can be understood as follows:

m.groupCount() denotes the number of () in the regular expression in Pattern.compile().

m.group(0), equivalent to m.group(). It means that the input sequence str matches the whole pattern, so it is cat

m.group(1) represents the content in the first parentheses of the matching regular expression, so it's ca. Note that it's also the first value.

m.group(2) represents the content in the second parentheses of the matching regular expression, so it's t. Note that it's also the first value.

groupcount() returns the number of capturing groupings of regular expressions (capturing groupings and non-capturing groupings are additional knowledge points), and the result of groupcount() does not indicate the matching result.

Group (0) refers to the whole string that satisfies the regular expression. Group (1) refers to what is in the first parentheses in the regular expression, and group (2) refers to what is in the second parentheses in the regular expression.

Sample code:

public class  MailTest{
    public static void main(String[] args) throws Exception{
        
        String regEx = "count(\\d+)(df)(df)*";  
        String s = "count000dfsdffcount123dfdfdfdfaaaa1";  //There are two matches
        Pattern pat = Pattern.compile(regEx);  
        Matcher mat = pat.matcher(s); 
        if(mat.find()){
           int groupCount = mat.groupCount();
           System.out.println(groupCount);
           for(int i = 0;i <= groupCount;i++) {
        	   System.out.println(mat.group(i));
           }
        }
        System.out.println();
        if(mat.find()){
            int groupCount = mat.groupCount();
            System.out.println(groupCount);
            for(int i = 0;i <= groupCount;i++) {
         	   System.out.println(mat.group(i));
            }
            //count(\\d+)(df)(df)*-->count(\\d+)(df)(df)(df)(df)
            //groupCount() captures only the number of groupings from the regular expression (the actual number of ()), so the following groups (4) and 5 report errors
            //System.out.println(mat.group(4)); // It seems that there are 4 in the second matching string
            //System.out.println(mat.group(5)); // It seems that there are 5 in the second matching string.
         }
    }
}

Output results:

3 //There are three parentheses count (\ d+) (df) (df)*) in a regular expression, so groupCount is always 3, and groupCount is just () in a regular expression.
count000df //The whole channeling of matching regular expressions
000 //The first parenthesis in a regular expression (\ d+)
df  //The second parenthesis (df) in a regular expression
null

3
count123dfdfdfdf
123
df
df

If there is no "()" number to group in a regular expression, then groupCount is 0, for example:

public class  MailTest{
    public static void main(String[] args) throws Exception{
        
        String regEx = "count\\d+";  
        String s = "count000dfsdffcount123dfdfdfdfaaaa1";  //There are two matches
        Pattern pat = Pattern.compile(regEx);  
        Matcher mat = pat.matcher(s); 
        if(mat.find()){
           int groupCount = mat.groupCount();
           System.out.println(groupCount);
           for(int i = 0;i <= groupCount;i++) {
        	   System.out.println(mat.group(i));
           }
        }
        System.out.println();
        if(mat.find()){
            int groupCount = mat.groupCount();
            System.out.println(groupCount);
            for(int i = 0;i <= groupCount;i++) {
         	   System.out.println(mat.group(i));
            }
         }
    }
}

Output results:

0
count000

0
count123

Posted by welshmike on Sat, 10 Aug 2019 04:04:46 -0700