在NFA中,很容易使所有以前非终止状态变为接受状态,从而使其匹配给定语言的所有子串。
在Java正则表达式引擎中,有没有一种方法可以找出一个字符串是否是与给定正则表达式匹配的字符串的起始子串?
表达式regexX ~“任何开始于”,regexA = 任何普通正则表达式
结果表达式“regexXregexA”匹配所有“regexA”的所有匹配项的起始子串:
例如:
在Java正则表达式引擎中,有没有一种方法可以找出一个字符串是否是与给定正则表达式匹配的字符串的起始子串?
表达式regexX ~“任何开始于”,regexA = 任何普通正则表达式
结果表达式“regexXregexA”匹配所有“regexA”的所有匹配项的起始子串:
例如:
regexA = a*b, matches "ab" and not "a"
"regexXa*b", matches "a" because it is a start of "ab" (and "aab")
编辑:
由于有些人仍然无法理解,这里提供一个程序测试来解答此问题:
import java.util.regex.*;
public class Test1 {
public static void main(String args[]){
String regex = "a*b";
System.out.println(
partialMatch(regex, "aaa");
);
}
public boolean partialMatch(String regex, String begining){
//return true if there is a string which matches the regex and
//startsWith(but not equal) begining, false otherwise
}
}
必须返回真。