Use Java regular expression

java.util.regex packet mainly includes the following three categories:

Pattern categories:

  Object pattern is a regular expression compiler representation. Pattern no public class constructor. To create a Pattern object, you must first call its public static compile method that returns a Pattern object. The method accepts a regular expression as its first parameter.

Matcher categories:

  Matcher object is input string matching operation will be explained and the engine. Like the Pattern class, Matcher no public constructor. You need to call the matcher method Pattern object to obtain a Matcher object.

PatternSyntaxException:

  PatternSyntaxException is a non-mandatory exception class that represents a syntax error in the regular expression pattern.

  In the following examples the regular expression * runoob * used to find whether a string contains the substring runoob.:

package cc.bcy;

java.util.regex import. * ;

public class RegexExample 
{
    public static void main(String[] args) 
    {
        String content="I am noob from runoob.com";
        String pattern=".*runoob.*";
        boolean isMatch=Pattern.matches(pattern, content);
        . The System OUT .println ( " string contains a 'runoob' substring? " + IsMatch);
    }
}
/*
String contains a 'runoob' substring? true
*/

Capture Group:

Capturing a plurality of characters when the group is a single method of processing unit, which is created by grouping the characters in the brackets. Can be viewed by groupCount method calls matcher object expression how many packets. groupCount method returns an int value representing a plurality of objects currently matcher capture group.

There is also a special group (group (0)), it always represents the entire expression. The group is not included in the return value groupCount.

package cc.bcy;

java.util.regex import. *;

public class RegexExample 
{
    public static void main(String[] args) 
    {
        String line="This order was placed for QT3000! OK?";
        String pattern="(\\D*)(\\d+)(.*)";
        // Create a Pattern object
        Pattern p=Pattern.compile(pattern);
        // Create Object Matcher
        Matches m = p.matcher (line);
        if(m.find())
        {
            System.out.println("Found value: "+m.group(0));
            System.out.println("Found value: "+m.group(1));
            System.out.println("Found value: "+m.group(2));
            System.out.println("Found value: "+m.group(3));
        }
        else
        {
            System.out.println("No Match!");
        }
        int n=m.groupCount();
        System.out.println ( "There are a" + n + "capturing group");
    }
}
/*
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT
Found value: 3000
Found value: ! OK?
A total of three capturing group
*/

Java regular expression syntax:

  In other languages, \\ said: I want to insert a regular expression backslash (literally) in the positive, do not give it any special significance. 

  In Java, \\ said: I want to insert a backslash a regular expression, so the following character has special meaning.

  So, in other languages ​​(such as Perl), a backslash \ is enough to have escaped role, and in Java regular expression in two backslash you need to have in order to be resolved to turn in other languages righteous action. Also can be simply understood in regular expressions Java, two \\ behalf of one of the other languages ​​\, which is why a digital representation of the regular expression is \\ d, which represents one ordinary backslash Yes.

Matcher class methods :

 

 

 

 The following is an example of the number of occurrences in the input string for the word occurrence count "cat":

package cc.bcy;

java.util.regex import. *;

public class RegexExample 
{
    private static final String REGEX="\\bcat\\b";
    private static final String INPUT="cat cat cat cattie cat";
    public static void main(String[] args) 
    {
        Pattern p=Pattern.compile(REGEX);
        Matcher m=p.matcher(INPUT);
        int count=0;
        while(m.find())
        {
            count++;
            System.out.println("Match number "+count);
            System.out.println("start() "+m.start());
            System.out.println("end() "+m.end());
            System.out.println();
        }
    }
}
/*
Match number 1
start() 0
end() 3

Match number 2
start() 4
end() 7

Match number 3
start() 8
end() 11

Match number 4
start() 19
end() 22
*/

lookingAt matches and methods used to attempt to match an input sequence pattern. They are different matches match requirements of the entire sequence, but is not required lookingAt. Although the method does not require lookingAt sentence match, but the need to start from the first character to match.

 

 The string matches all replaced by "-" with the appendReplacement :( () method)

package cc.bcy;

java.util.regex import. *;

public class RegexExample 
{
    private static final String REGEX="a*b";
    private static final String INPUT="aabfooaabfooabfoobkkk";
    public static void main(String[] args) 
    {
        Pattern p=Pattern.compile(REGEX);
        Matcher m=p.matcher(INPUT);
        StringBuffer sb=new StringBuffer();
        while(m.find())
        {
            m.appendReplacement(sb, "-");
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    }
}
/*
-foo-foo-foo-kkk
*/

replaceFirst 和 replaceAll 方法用来替换匹配正则表达式的文本。不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配。

package cc.bcy;

import java.util.regex.*;

public class RegexExample 
{
    private static final String REGEX="dog";
    private static final String INPUT="The dog says meow. All dogs say meow.";
    public static void main(String[] args) 
    {
        Pattern p=Pattern.compile(REGEX);
        Matcher m=p.matcher(INPUT);
        String str=m.replaceAll("cat");
        System.out.println(str);
    }
}
/*
The cat says meow. All cats say meow.
*/

PatternSyntaxException 类的方法:

  PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误。 PatternSyntaxException 类提供了下面的方法来帮助我们查看发生了什么错误。

 

Guess you like

Origin www.cnblogs.com/Koaler/p/12317514.html