为了账号安全,请及时绑定邮箱和手机立即绑定

检查字符串是否包含正则表达式

检查字符串是否包含正则表达式

C#
慕桂英4014372 2021-09-19 18:46:14
我有一个文本字符串,我想检查该字符串是否包含某个模式。即 [Ref:number/number/number]var myStr = "This is a string [Ref:1234/823/2]";  //Yes, has regex patternvar myStr2 = "This is another sample string"; //No, regex pattern not present任何想法该模式的正则表达式是:[Ref:1234/823/2]?单词 Ref 将始终出现,后跟一个冒号,然后是由正斜杠分隔的 3 组数字,并将包含在方括号内。[Ref:<digits>/<digits>/<digits>]有任何想法吗?
查看完整描述

2 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

   static void Main(string[] args){


        Regex rx = new Regex(@"\[Ref:(-?[0-9]+)/(-?[0-9]+)/(-?[0-9]+)\]");       

        string text = "This is a string [Ref:1234/823/2]";

        MatchCollection matches = rx.Matches(text);


        foreach (Match match in matches)

        {

            GroupCollection groups = match.Groups;

            int first_value = Int32.Parse(groups[1].Value);

            int second_value = Int32.Parse(groups[2].Value);

            int third_value = Int32.Parse(groups[3].Value);

        }

    }

(编辑)如果您不需要这些值:


    static void Main(string[] args){

        Regex rx = new Regex(@"\[Ref:(-?[0-9]+)/(-?[0-9]+)/(-?[0-9]+)\]");       

        string text = "This is a string [Ref:1234/823/2]";

        bool matched = rx.IsMatch(text);

    }


查看完整回答
反对 回复 2021-09-19
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

我没有在c#,有时这个模式会帮助你找到那个。


[Ref:[\d+/\d+/\d]+]


查看完整回答
反对 回复 2021-09-19
  • 2 回答
  • 0 关注
  • 157 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信