【问题标题】:How to allow space in regex?如何在正则表达式中留出空间?
【发布时间】:2016-01-05 21:33:17
【问题描述】:

我试图在 New : 双引号中获取值。 当 ListName 中没有空格时,我可以很好地检索该值。但是,如果我在列表名称之间放置空格(例如 NewFinancial History:\"xyz\"),则会引发以下错误:

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

它在下面一行抛出错误 var matches = Regex.Matches(contents, regex, 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);
        }            
    }
}

【问题讨论】:

标签: c# regex


【解决方案1】:

我不明白你为什么还要使用“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);
        }            
    }
}

【讨论】:

  • 在我上面的代码中,它还搜索一个带空格的新单词,例如字符串内容 = " testing New Financial History:\"xyz\" and NewCompany Type:"DealCloud" test ";我想搜索没有空格的关键字,例如从上面的字符串中它应该只找到 NewCompany Type 而不是 New Financial History。我使用了与上述相同的方法。
  • 如果你想在 New 之后排除空格,你应该使用这个正则表达式:@"New\w(.+?):" 这意味着在 New 之后有一个字母数字字符(或下划线)
【解决方案2】:

将你的 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 demo

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

【讨论】:

  • 在我上面的代码中,它还搜索带有空格的新单词,例如字符串内容 = " testing New Financial History:\"xyz\" and NewCompanies:"DealCloud" test ";我想搜索没有空格的关键字,例如从上面的字符串中它应该只找到 NewCompanies 而不是 New Financial History 我使用了上述相同的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2014-12-31
  • 2016-08-24
  • 2011-02-20
  • 2018-11-19
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多