字符串格式检查给出意外结果(正则表达式)

时间:2022-09-06 18:46:31

I want to check format of a string which is ABC-nnn, where ABC represents alphabets (English) in capital letters. nnn represents triple digit number for example 123 or 001 or 012 A complete example would be FBI-026. I used regex for that and below is my code.

我想检查一个ABC-nnn字符串的格式,其中ABC代表大写字母的字母(英文)。 nnn表示三位数,例如123或001或012一个完整的例子是FBI-026。我使用正则表达式,下面是我的代码。

public bool IsSubstringMatchingFormat(int numberOfLettersBeforeHyphen, int numberOfNumbersAfterHyphen, string stringToMatch)
{
    Regex regex = new Regex($@"^[A-Z]{numberOfLettersBeforeHyphen}-\d{numberOfNumbersAfterHyphen}");
    return regex.IsMatch(stringToMatch);
} 

I call it IsSubstringMatchingFormat(3, 3, "SMB-123") but it returns false. Please provide your insight.

我称之为IsSubstringMatchingFormat(3,3,“SMB-123”),但它返回false。请提供您的见解。

3 个解决方案

#1


4  

Have you actually checked what the string you are passing into the regex looks like? ie evaluate $@"^[A-Z]{numberOfLettersBeforeHyphen}-\d{numberOfNumbersAfterHyphen}"and see if that is the regex you want? I can tell you that it isn't because it will end up being ^[A-Z]3-\d3 which does not do what you want.

你真的检查了你传入正则表达式的字符串是什么样的吗?即评估$ @“^ [A-Z] {numberOfLettersBeforeHyphen} - \ d {numberOfNumbersAfterHyphen}”并查看这是否是您想要的正则表达式?我可以告诉你,这不是因为它最终会成为^ [A-Z] 3- \ d3,它不会做你想要的。

What I think you'll want is:

我认为你想要的是:

$@"^[A-Z]{{{numberOfLettersBeforeHyphen}}}-\d{{{numberOfNumbersAfterHyphen}}}"

This adds the escaped curly braces back into your regex to give:

这会将转义的花括号添加回正则表达式中,以便:

^[A-Z]{3}-\d{3}

The equivalent of this using String.Format would be:

使用String.Format相当于:

String.Format(
    @"^[A-Z]{{{0}}}-\d{{{1}}}",
    numberOfLettersBeforeHyphen, 
    numberOfLettersAfterHyphen);

#2


0  

Your curly braces are being consumed by the string interpolation and are not making it into the regex. If you try to print the regex, you'll see it's something like

你的花括号被字符串插值所消耗,并没有进入正则表达式。如果你试图打印正则表达式,你会发现它是这样的

^[A-Z]3-\d3

^ [A-Z] -3- \ D3

Which is something else entirely.

这完全是另一回事。

#3


0  

The {} are being removed when you format. Try this:

格式化时将删除{}。尝试这个:

        public static bool IsSubstringMatchingFormat(int numberOfLettersBeforeHyphen, int numberOfNumbersAfterHyphen, string stringToMatch)
    {
        var expre = @"^[A-Z]{numberOfLettersBeforeHyphen}-\d{numberOfNumbersAfterHyphen}";//
        expre = expre.Replace("numberOfLettersBeforeHyphen", numberOfLettersBeforeHyphen.ToString())
            .Replace("numberOfNumbersAfterHyphen", numberOfNumbersAfterHyphen.ToString());
        Regex regex = new Regex(expre);
        return regex.IsMatch(stringToMatch);
    } 

#1


4  

Have you actually checked what the string you are passing into the regex looks like? ie evaluate $@"^[A-Z]{numberOfLettersBeforeHyphen}-\d{numberOfNumbersAfterHyphen}"and see if that is the regex you want? I can tell you that it isn't because it will end up being ^[A-Z]3-\d3 which does not do what you want.

你真的检查了你传入正则表达式的字符串是什么样的吗?即评估$ @“^ [A-Z] {numberOfLettersBeforeHyphen} - \ d {numberOfNumbersAfterHyphen}”并查看这是否是您想要的正则表达式?我可以告诉你,这不是因为它最终会成为^ [A-Z] 3- \ d3,它不会做你想要的。

What I think you'll want is:

我认为你想要的是:

$@"^[A-Z]{{{numberOfLettersBeforeHyphen}}}-\d{{{numberOfNumbersAfterHyphen}}}"

This adds the escaped curly braces back into your regex to give:

这会将转义的花括号添加回正则表达式中,以便:

^[A-Z]{3}-\d{3}

The equivalent of this using String.Format would be:

使用String.Format相当于:

String.Format(
    @"^[A-Z]{{{0}}}-\d{{{1}}}",
    numberOfLettersBeforeHyphen, 
    numberOfLettersAfterHyphen);

#2


0  

Your curly braces are being consumed by the string interpolation and are not making it into the regex. If you try to print the regex, you'll see it's something like

你的花括号被字符串插值所消耗,并没有进入正则表达式。如果你试图打印正则表达式,你会发现它是这样的

^[A-Z]3-\d3

^ [A-Z] -3- \ D3

Which is something else entirely.

这完全是另一回事。

#3


0  

The {} are being removed when you format. Try this:

格式化时将删除{}。尝试这个:

        public static bool IsSubstringMatchingFormat(int numberOfLettersBeforeHyphen, int numberOfNumbersAfterHyphen, string stringToMatch)
    {
        var expre = @"^[A-Z]{numberOfLettersBeforeHyphen}-\d{numberOfNumbersAfterHyphen}";//
        expre = expre.Replace("numberOfLettersBeforeHyphen", numberOfLettersBeforeHyphen.ToString())
            .Replace("numberOfNumbersAfterHyphen", numberOfNumbersAfterHyphen.ToString());
        Regex regex = new Regex(expre);
        return regex.IsMatch(stringToMatch);
    }