正则表达式过滤器字符串编号和编号不起作用
Regex filter string number and number not working
我正在尝试使用正则表达式提取格式为 "[\r\n \"MG480612230220150018\"\r\n]" 的字符串,我是尝试匹配最小长度为 5 个字符的数字和字母表,但它不起作用,因此我可以保证我会提取此数据 (MG480612230220150018)
Regex regex = new Regex(@"^[0-9a-zA-Z]{5,}$");
Match match = regex.Match(availability.Id.ToString());
if (match.Success)
{
var myid = match.Value;
}
目前,您正在匹配字符串的开头和结尾。如您所说,输入字符串较长[\r\n \"MG480612230220150018\"\r\n]
。所以,你需要删除锚点:
Regex regex = new Regex(@"[0-9a-zA-Z]{5,}");
您将获得匹配 (MG480612230220150018
)。
看看demo。
作为替代方案,在 C# 中,我会使用 Unicode 类 来匹配字符:
Regex regex = new Regex(@"[\p{N}\p{L}]{5,}");
\p{N}
代表Unicode数字,\p{L}
代表任何Unicode字母,不区分大小写。
这对你有用:
Regex regex = new Regex(@"[a-z\d]{5,}", RegexOptions.IgnoreCase);
正则表达式解释:
[a-z\d]{5,}
Options: Case insensitive
Match a single character present in the list below «[a-z\d]{5,}»
Between 5 and unlimited times, as many times as possible, giving back as needed (greedy) «{5,}»
A character in the range between “a” and “z” (case insensitive) «a-z»
A “digit” (any decimal number in any Unicode script) «\d»
我正在尝试使用正则表达式提取格式为 "[\r\n \"MG480612230220150018\"\r\n]" 的字符串,我是尝试匹配最小长度为 5 个字符的数字和字母表,但它不起作用,因此我可以保证我会提取此数据 (MG480612230220150018)
Regex regex = new Regex(@"^[0-9a-zA-Z]{5,}$");
Match match = regex.Match(availability.Id.ToString());
if (match.Success)
{
var myid = match.Value;
}
目前,您正在匹配字符串的开头和结尾。如您所说,输入字符串较长[\r\n \"MG480612230220150018\"\r\n]
。所以,你需要删除锚点:
Regex regex = new Regex(@"[0-9a-zA-Z]{5,}");
您将获得匹配 (MG480612230220150018
)。
看看demo。
作为替代方案,在 C# 中,我会使用 Unicode 类 来匹配字符:
Regex regex = new Regex(@"[\p{N}\p{L}]{5,}");
\p{N}
代表Unicode数字,\p{L}
代表任何Unicode字母,不区分大小写。
这对你有用:
Regex regex = new Regex(@"[a-z\d]{5,}", RegexOptions.IgnoreCase);
正则表达式解释:
[a-z\d]{5,}
Options: Case insensitive
Match a single character present in the list below «[a-z\d]{5,}»
Between 5 and unlimited times, as many times as possible, giving back as needed (greedy) «{5,}»
A character in the range between “a” and “z” (case insensitive) «a-z»
A “digit” (any decimal number in any Unicode script) «\d»