[英]Issue with a regex pattern in C#
I have an issue with regex pattern in C# programming language.我在 C# 编程语言中遇到了正则表达式模式的问题。 My regex pattern doesn't seem to work the way it does on Regexr, I am getting matches even for values not empty or not in the list, where my regex is intended for a specific list of values and an empty string.
我的正则表达式模式似乎不像在 Regexr 上那样工作,即使值不为空或不在列表中,我也得到匹配,其中我的正则表达式用于特定的值列表和空字符串。 Below is the piece of code which could demonstrate the essence of the issue:
下面是一段可以证明问题本质的代码:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var pattern = "^(2|4|First|Fourth)?";
var regex = new Regex(pattern);
var empty = "";
var number = "3";
var yes = "yes";
var str = "str";
Console.WriteLine("empty " + regex.IsMatch(empty));
Console.WriteLine("number " + regex.IsMatch(number));
Console.WriteLine("yes " + regex.IsMatch(yes));
Console.WriteLine("string " + regex.IsMatch(str));
}
}
It matches str
and 3
when it shouldn't.它不应该匹配
str
和3
。 Maybe you could suggest some solution?也许您可以提出一些解决方案?
Pattern (2|4|First|Fourth)
will match 2
or 4
or First
or Fourth
, you are correct, ^
mathes beginning of the string (so called anchor), but what you are not aware of is ?
Pattern
(2|4|First|Fourth)
将匹配2
或4
或First
或Fourth
,你是对的, ^
计算字符串的开头(所谓的锚点),但你不知道的是?
operator, which means **match zero or one occurence of a pattern , so, since it's applied to whole
(2|4|First|Fourth)` pattern, it will match every string.运算符,这意味着 **匹配模式的零次或一次出现
, so, since it's applied to whole
(2|4|First|Fourth) 模式,它将匹配每个字符串。
You can think of this as: match my pattern anchored at the beginning of hte string, but match it zero or one time, so, also match just beginning of a string .您可以将其视为:匹配锚定在 hte 字符串开头的我的模式,但匹配它零次或一次,因此,也匹配字符串的开头。
See in Demo , that every string is matched (it matches beginning of a string).在Demo中看到,每个字符串都是匹配的(它匹配字符串的开头)。
Just remove ?
只是删除
?
operator or replace it with $
, which matches end of a string (if it's desired).运算符或将其替换为
$
,它匹配字符串的结尾(如果需要)。
To allow also empty string use ^(2|4|First|Fourth|)$
- it will anchor the end of the string with $
(as mentioned above), also another possibility is added in your alternation, which will allow empt string to match.还允许使用空字符串
^(2|4|First|Fourth|)$
- 它将用$
锚定字符串的末尾(如上所述),在您的交替中还添加了另一种可能性,这将允许空字符串匹配。
If you want to match empty or multiple options(as you mentioned in one of the comments ), there is the way to go:如果您想匹配空选项或多个选项(正如您在评论之一中提到的),可以使用 go 的方法:
"^(|2|4|First|Fourth)$"
It will match empty, "2", "4", "First" or "Fourth"
.它将匹配
empty, "2", "4", "First" or "Fourth"
。
The problem with the pattern you have proposed is that ?
您提出的模式的问题是
?
makes whole group optional so it will match anything:).使整个组可选,因此它将匹配任何内容:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.