正则表达式中的C#字符串格式占位符

A. *_*ray 6 regex formatting

我有一个正则表达式,以逐字C#字符串类型定义,如下所示:

private static string importFileRegex = @"^{0}(\d{4})[W|S]\.(csv|cur)";
Run Code Online (Sandbox Code Playgroud)

在正则表达式行开始之后的前3个字母(^)可以是字母字符的许多可能组合之一.

我想使用上面的方法做一个优雅的String.Format,在开始时放置我的3个字母组合选择并在我的匹配算法中使用它,如下所示:

string regex = String.Format(importFileRegex, "ABC");
Run Code Online (Sandbox Code Playgroud)

哪个会给我一个正则表达式 ^ABC(\d{4})[W|S]\.(csv|cur)

问题是,当我执行String.Format时,因为我在字符串中有其他花括号(例如\d{4})String.Format寻找要放在这里的东西,找不到它并给我一个错误:

System.FormatException : Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Run Code Online (Sandbox Code Playgroud)

任何人都知道,如果没有拆分弦,我可以逃避其他花括号或东西,以避免上述错误?

And*_*are 10

试试这个(注意双花括号):

@"^{0}(\d{{4}})[W|S]\.(csv|cur)"