.net中的Reg表达式以2个特定字符开头,以数字结尾并具有一定长度

时间:2019-05-28 14:56:42

标签: .net regex

好的Reg Reg专家,我对regex不太满意,希望能有所帮助。我有一个正则表达式,我一生都无法弄清楚。我正在寻找创建一个符合以下条件的正则表达式:

以“ PA”开头(ingore情况)

以数字结尾

长度为8个字符(忽略任何尾随空白)

以“ WN”(ingore大小写)开头

以数字结尾

长达10个字符(忽略任何尾随空白)

1 个答案:

答案 0 :(得分:1)

//Trim the whitespace off the ends of your string per requirement
yourString = yourString.Trim();
//Declare regex, the pattern tells it to look for any 7 letter word
//which starts with PA and ends with a digit and is 7 characters long
//OR a word which starts with WN and ends with a digit and is 10 characters long. 
Regex regex = new Regex(^PA.+\d${7})|(^WN.+\d${10});
//Set the regex option to ignore case
RegexOptions options = RegexOptions.IgnoreCase;
//Get the match collection by passing your string, the regex pattern and
//the regex options
MatchCollection matches = regex.Matches(yourString, regex, options);

//Do something with captured text