Regular Expression Matching

来源:互联网 发布:矩阵连乘a1 a2 编辑:程序博客网 时间:2024/05/17 08:49

'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true

偷个懒:

import java.util.regex.Pattern;import java.util.regex.Matcher;public class Solution{public boolean isMatch(String s, String p) {Pattern pattern = Pattern.compile(p);Matcher matcher = pattern.matcher(s);return matcher.matches();    }}


0 0
原创粉丝点击