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

时间:2017-12-22 09:53:33

标签: c# .net regex

我想检查ABC-nnn字符串的格式,其中ABC代表大写字母的字母(英文)。 nnn代表三位数字,例如123001012完整示例为FBI-026。我使用了regex,下面是我的代码。

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

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

3 个答案:

答案 0 :(得分:4)

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

我认为你想要的是:

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

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

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

使用String.Format的相当于:

String.Format(
    @"^[A-Z]{{{0}}}-\d{exports.write = functions.https.onRequest((req, res) => {
    admin.database()
        .ref(`xxx/yyy`)
        .push()
        .set({timestamp: admin.database.ServerValue.TIMESTAMP});
    res.status(200).end();
});
}",
    numberOfLettersBeforeHyphen, 
    numberOfLettersAfterHyphen);

答案 1 :(得分:0)

你的花括号被字符串插值所消耗,并没有进入正则表达式。如果您尝试打印正则表达式,您会看到类似

的内容

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E0E0E0"> <RelativeLayout android:id="@+id/top_card" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="4dp"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="200dp" android:alpha="1.0" android:scaleType="centerCrop" tools:src="@drawable/food_1" /> <View android:layout_width="match_parent" android:layout_height="200dp" android:background="@drawable/gradient_up" /> <!-- Back button --> </RelativeLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_show_rating_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/top_card" android:layout_alignParentRight="true" android:layout_marginBottom="-28dp" android:layout_marginRight="16dp" app:srcCompat="@drawable/ic_add_white_24px" /> <!-- Ratings --> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/top_card" android:background="@android:color/transparent" android:clipToPadding="false" android:paddingBottom="16dp" android:paddingTop="28dp" android:visibility="gone" tools:listitem="@layout/item_rating" /> </RelativeLayout>

完全是另一回事。

答案 2 :(得分:0)

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

        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);
    }