文本的Regex表达式
本文关键字:表达式 Regex 文本 | 更新日期: 2023-09-27 18:20:46
如何为前两个字符是字母,但其余字符是数字的文本编写regex表达式?
假设这是唯一的必要条件。
[a-zA-Z]{2,2}'d+
这将是:
'[A-Za-z]{2}'d+
Schimples!
下面是代码。
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string txt="aa4"; //for instance.
string re1="([a-zA-Z])";
string re2="([a-zA-Z])";
string re3="(''d+)";
Regex r = new Regex(re1+re2+re3,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String w1=m.Groups[1].ToString();
String w2=m.Groups[2].ToString();
String d1=m.Groups[3].ToString();
Console.Write("("+w1.ToString()+")"+"("+w2.ToString()+")"+"("+d1.ToString()+")"+"'n");
}
Console.ReadLine();
}
}
}