【发布时间】:2019-05-28 14:56:41
【问题描述】:
Ok Reg ex 专家,我不太擅长正则表达式,希望能得到一些帮助。我有一个正则表达式,我一生都无法弄清楚。我希望创建一个匹配以下内容的正则表达式:
以“PA”开头(ingore 大小写)
以数字结尾
长度为 8 个字符(忽略任何尾随空格)
或
以“WN”开头(ingore 大小写)
以数字结尾
长度为 10 个字符(忽略任何尾随空格)
【问题讨论】:
-
说实话我什至不知道从哪里开始
Ok Reg ex 专家,我不太擅长正则表达式,希望能得到一些帮助。我有一个正则表达式,我一生都无法弄清楚。我希望创建一个匹配以下内容的正则表达式:
以“PA”开头(ingore 大小写)
以数字结尾
长度为 8 个字符(忽略任何尾随空格)
或
以“WN”开头(ingore 大小写)
以数字结尾
长度为 10 个字符(忽略任何尾随空格)
【问题讨论】:
//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
【讨论】:
RegexOptions.IgnoreCase 行,并在第一行修剪空白处,也调用ToLower(),所以yourString = yourString.ToLower().Trim(); 而且,欢迎您回答,我希望对您有所帮助。