正则表达式

一、The Regular Expressions API has three core classes that you use almost all the time:
     
  1.public final class Pattern extends Object implements Serializable
           A compiled representation of a regular expression.   一个正则表达式的编译表示
     
     2.public final class Matcher extends Object     implements MatchResult
           An engine that performs match operations on a character sequence by interpreting a Pattern.   一个引擎通过解释Pattern执行匹配操作字符序列。

A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

  • The matches method attempts to match the entire input sequence against the pattern.

  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.

  • The find method scans the input sequence looking for the next subsequence that matches the pattern.
     
  3.public class PatternSyntaxException     extends IllegalArgumentException
          Unchecked exception thrown to indicate a syntax error in a regular-expression pattern.   未经检查的异常来表示一个正则表达式模式的语法错误。
 
总结:
  Pattern describes a string pattern.
  Matcher tests a string to see if it matches the pattern.

  PatternSyntaxException tells you that something wasn't acceptable about the pattern that you tried to define.

二、Regex pattern syntax

     A regex pattern describes the structure of the string that the expression tries to find in an input string.

    .          Any character?Zero (0) or one (1) of what came before
    *         Zero (0) or more of what came before+One (1) or more of what came before
    []        A range of characters or digits^Negation of whatever follows (that is, "not whatever")
    d        Any digit (alternatively, [0-9])DAny nondigit (alternatively, [^0-9])
    s        Any whitespace character (alternatively, [ f ])
    S        Any nonwhitespace character (alternatively, [^ f ])
    w        Any word character (alternatively, [a-zA-Z_0-9])
    W       Any nonword character (alternatively, [^w])

三、test

  @Test
    public void test() {
      //The matches() method in the Matcher class matches the regular expression against the whole text 

      //The methods start() and end() will give the indexes into the text where the found match starts and ends. 
      //Find a string of the form A or a followed by zero or more characters, followed by string.
        Pattern pattern = Pattern.compile("[Aa].*string");
        Matcher matcher = pattern.matcher("A string");
        boolean didMatch = matcher.matches();
        System.out.println(didMatch);
        int patternStartIndex = matcher.start();
        System.out.println(patternStartIndex);
        int patternEndIndex = matcher.end();
        System.out.println(patternEndIndex);
    }

---------

true
0
8

@Test
    public void test1() {
        //lookingAt()
        //If your string had more elements than the number of characters in the pattern you searched for,
        // you could use lookingAt() instead of matches().
        // The lookingAt() method searches for substring matches for a specified pattern.
        Pattern pattern = Pattern.compile("[Aa].*string");
        Matcher matcher = pattern.matcher("A string with more than just the pattern.");
        boolean didMatch = matcher.lookingAt();
        System.out.println(didMatch);
    }

---------

true

@Test
    public void test2() {
        //find()
    //if multiple matches can be found in the text, the find() method will find the first, and then for each subsequent call to find() it will move to the next match.
String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord."; Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { Logger.getAnonymousLogger().info("Found this wiki word: " + matcher.group()); } }

--------

Found this wiki word: WikiWord

Found this wiki word: AnotherWikiWord

Found this wiki word: SomeWikiWord

参考文章:

  http://tutorials.jenkov.com/java-regex/index.html

  https://www.ibm.com/developerworks/library/j-perry-regular-expressions/index.html

 
原文地址:https://www.cnblogs.com/parkdifferent/p/5916446.html