C#利用正则表达式获取特定格式的字串符

   /// 
    /// return the first match string. use regularExpression. eg.  getMatchStr("abc1233dfdfa456","\d+")
    /// 
    /// string
    /// reg string
    /// 
    public string  getMatchStr(string str,string regular)
    {           
        MatchCollection matchCol = Regex.Matches(str, regular);

        if (matchCol.Count > 0)
        {
            return Convert.ToString(matchCol[0]);//这个例子是取第一个匹配的字符串
        }
        else
            return "";
    }

你可能感兴趣的:(基础知识)