如何在regex中留出空间

本文关键字:空间 regex | 更新日期: 2023-09-27 18:29:02

我正在尝试获取双引号中New:之后的值。当ListName中没有空格时,我可以很好地检索值。但是,如果我在列表名称之间加空格(例如,NewFinancial History:''"xyz''"),它会抛出以下错误:

正在分析"NewFinancial History:"(?[^"]*)"-无效的组名:组名必须以单词字符开头。

它在底线处抛出错误var matches=Regex.matches(内容、正则表达式、RegexOptions.Singleline);

下面是我的代码。

string contents = " testing NewFinancial History:'"xyz'"   ";
var keys = Regex.Matches(contents, @"New(.+?):", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace).OfType<Match>().Select(m => m.Groups[0].Value.Trim().Replace(":", "")).Distinct().ToArray();
foreach (string key in keys)
{
    List<string> valueList = new List<string>();
    string listNameKey = key;
    string regex = "" + listNameKey + ":" + "'"(?<" + listNameKey + ">[^'"]*)'"";
    var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
    foreach (Match match in matches)
    {
        if (match.Success)
        {                    
            string value = match.Groups[key].Value;
            valueList.Add(value);
        }            
    }
}

如何在regex中留出空间

我不明白为什么还要使用"key"作为组的名称。

您遇到的问题是组名称不能包含空格,但您可以简单地创建一个匿名组。

string contents = " testing NewFinancial History:'"xyz'"   ";
var keys = Regex.Matches(contents, @"New(.+?):", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace).OfType<Match>().Select(m => m.Groups[0].Value.Trim().Replace(":", "")).Distinct().ToArray();
foreach (string key in keys)
{
    List<string> valueList = new List<string>();
    string listNameKey = key;
    string regex = "" + listNameKey + ":" + "'"([^'"]*)'"";  //create an anonymous capture group
    var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
    foreach (Match match in matches)
    {
        if (match.Success)
        {                    
            string value = match.Groups[0].Value; //get the first group
            valueList.Add(value);
        }            
    }
}

将foreach块更改为

List<string> valueList = new List<string>();
string listNameKey = key;
string regex = "" + listNameKey + ":" + "'"(?<" + 
        listNameKey.Replace(" ","") + ">[^'"]*)'""; // Removing spaces in the group name here
var matches = Regex.Matches(contents, regex, RegexOptions.Singleline);
foreach (Match match in matches)
{
    if (match.Success)
    {                    
        string value = match.Groups[key.Replace(" ", "")].Value; // Removing spaces here
        valueList.Add(value);
    }            
}

关键是,组名称不能有空格,因此需要在声明捕获组名称的地方用空字符串替换它们。

查看IDEONE演示

请注意,您的New(.+?):正则表达式没有可忽略的空白,我建议删除RegexOptions.IgnorePatternWhitespace标志。您可以将其替换为更高效的New([^:]+):