代码之家  ›  专栏  ›  技术社区  ›  user726720

删除字符并在数字之间选择的regex

  •  -3
  • user726720  · 技术社区  · 6 年前

    我有一个以下字符串,我需要使用c_应用regex:

    2018-12-26P18:07:05:07
    

    我只需要挑18号和07号。所以我需要的结果是1807年

    我试过

    \d+-\d+-\d+P
    

    这给我去掉了 2018-12-26P . 现在我该怎么移除 : 然后只挑选 1807

    1 回复  |  直到 6 年前
        1
  •  1
  •   BladeMight    6 年前

    下面是简单的正则表达式组用法:

    using System;
    using System.Text.RegularExpressions;
    
    namespace RE {
        class TEST {
            static void Main(string[] args) {
                // Your string
                var str = "2018-12-26P18:07:05:07";
                // Regex to match 18 and 07 to 1 and second group.
                var re = new Regex(@"\d+-\d+-\d+[A-z](\d+):(\d+)");
                // Execute regex over string, and get our matched groups
                var match = re.Match(str);
                // Write the groups.
                Console.WriteLine(match.Groups[1].Value + match.Groups[2].Value);
            }
        }
    }