Partial Matching of Regular Expressions

后端 未结 2 742
天命终不由人
天命终不由人 2020-12-20 23:40

In NFA it is easy to make all previously non-final states accepting to make it match language of all substrings of a given language.

In Java regex engine, is there

2条回答
  •  我在风中等你
    2020-12-21 00:02

    What you're looking for is called partial matching, and it's natively supported by the Java regex API (for the record, other engines which offer this feature include PCRE and boost::regex).

    You can tell if an input string matched partially by inspecting the result of the Matcher.hitEnd function, which tells if the match failed because the end of the input string was reached.

    Pattern pattern = Pattern.compile("a*b");
    Matcher matcher = pattern.matcher("aaa");
    System.out.println("Matches: " + matcher.matches());
    System.out.println("Partial match: " + matcher.hitEnd());
    

    This outputs:

    Matches: false
    Partial match: true
    

提交回复
热议问题