正则表达式匹配Version:WWW_G989D_1.1.0521中的_1.1.0521部分,并且替换掉_1.1.0521的_和. 最终结果 110521
代码如下:
using System;
using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;namespace ConsoleRegularExpression
{ class Program { static void Main(string[] args) { //测试正则表达式 Regex regex = new Regex(@"_\d+(.\d+)+"); string a = "Version:WWW_G989D_1.1.0521"; string b = "Version:WWW_G989D_122.122.0521.05211.1.0521"; bool result = regex.IsMatch("Version:WWW_G989D_1.1.0521"); Console.WriteLine($"{ result}"); foreach (var item in regex.Matches(a)) { Console.WriteLine($"{item}"); Regex regex1 = new Regex(@"[_.]"); string aa = item.ToString(); //Console.WriteLine(regex1.Replace(aa, "#")); Console.WriteLine(regex1.Replace(aa, "")); } Console.ReadLine(); } }}效果如下