【发布时间】:2018-10-22 10:09:51
【问题描述】:
目标找到一个完全匹配的单词,该单词正好是 7 个字符,在一个句子中以 3 个字母开头,后跟 4 个数字。
有效输入: ABC1234、MOW0912、qbc1239
我尝试使用设置长度
var result = Regex.Match(myString, "[A-Za-z]{3}[0-9]{4}\\d{9}");
我参考了几个类似的帖子,但没有帮助我解决问题
C# Regular Expression Help Needed
代码:目前为止
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string[] data = { "This, is a test ,* P.O ABC12 and ABCDE5134 this is some random words UserID ABC1234 that the users may have, regards, and the end of it" };
foreach (string myString in data)
{
if (Regex.IsMatch(myString, "[A-Za-z]{3}[0-9]{4}"))
{
var result = Regex.Match(myString, "[A-Za-z]{3}[0-9]{4}");
Console.WriteLine("{0} matches", myString);
Console.WriteLine("result is" + result);
}
else
{
Console.WriteLine("does not match");
}
}
}
}
输出
【问题讨论】:
-
为什么需要正则表达式来处理这种简单的情况?
-
您的代码到底有什么问题?这里还没有问题。