LeetCode---正则表达式系列




#10 正则表达式匹配

https://leetcode-cn.com/problems/regular-expression-matching/

本来想找用C#正则表达式做的题,而这道题是需要自己写正则。。。
下面那道是正则匹配问题

        public static bool IsMatch(string s, string p)
        {
            if (s == null || p == null)
                return false;

            return IsMatchCal(s, 0, p, 0);
        }

        public static bool IsMatchCal(string s, int sStart, string p, int pStart)
        {
            //是否比较到了最后
            if (pStart == p.Length) return sStart == s.Length;

            //第一个字符是否比对成功
            bool first_match = (
                sStart < s.Length &&
                (p[pStart] == s[sStart] || p[pStart] == '.')
            );

            //有*
            if (p.Length - pStart >= 2 && p[pStart + 1] == '*')
            {
                //aab  c*a*b
                //missss   mis*
                return (IsMatchCal(s, sStart, p, pStart + 2) ||
                        (first_match && IsMatchCal(s, sStart + 1, p, pStart)));
            }
            //无*
            else
            {
                //直接匹配下一个
                return first_match && IsMatchCal(s, sStart + 1, p, pStart + 1);
            }
        }

#65 有效数字

https://leetcode-cn.com/problems/valid-number/

正则表达式匹配

    public bool IsNumber(string s) {
        Regex regex = new Regex(@"^s*[+-]?(d+|(?=.d))(.d*)?(e[+-]?d+)?s*$");

        return regex.IsMatch(s);
    }

解析https://www.cnblogs.com/Fflyqaq/p/13040069.html

原文地址:https://www.cnblogs.com/Fflyqaq/p/13051600.html