简体   繁体   English

C#中的正则表达式问题仅返回单个匹配项

[英]Problems with regex in c# only returning a single match

I'm building a regex and I'm missing something as it's not working properly. 我正在建立一个正则表达式,但缺少一些东西,因为它无法正常工作。 my regex logic is trying to look for anything that has # anychars # and return the number of matches on the sentence and not a single match. 我的正则表达式逻辑试图查找具有# anychars #的任何内容,并返回句子中匹配的数量,而不是单个匹配。 Here are a few examples 这里有一些例子

1- #_Title_# and #_Content_# should return two matches: #_Title_# and #_Content_# . 1- #_Title_# and #_Content_#应该返回两个匹配项: #_Title_# and #_Content_#

2- Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_# 2- Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_# Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_# should return 3 matches: #_TemplateName_# #_Full_Product_Name_# and #_Short_Description_# Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#应该返回3场比赛: #_TemplateName_# #_Full_Product_Name_##_Short_Description_#

and so on. 等等。 Here is what my regex looks like: ^(.*#_.*_#.*)+$ 这是我的正则表达式: ^(.*#_.*_#.*)+$

any thoughts on what I'm doing wrong? 对我在做什么错有任何想法吗?

Something as simple as: 像这样简单:

#.*?#

Or: 要么:

#_.*?_#

If you are trying to match the underscores too (it wasn't clear in the original version of the question). 如果您也尝试匹配下划线(问题的原始版本尚不清楚)。 Or: 要么:

#_(.*?)_#

Which makes it easier to extract the token between your #_ and _# delimiters as a group. 这样可以更轻松地将#__#分隔符之间的标记作为组提取。

Should work. 应该管用。 The *? *? is key. 是关键。 It's non-greedy . 不是贪婪的 Otherwise you match everything between the first and last # 否则,您将匹配第一个和最后一个#之间的所有内容

So for example: 因此,例如:

var str = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#";

var r = new Regex("#_(.*?)_#");

foreach (Match m in r.Matches(str)) 
{
    Console.WriteLine(m.Value + "\t" + m.Groups[1].Value);
}

Outputs: 输出:

#_TemplateName_#         TemplateName
#_Full_Product_Name_#    Full_Product_Name
#_Short_Description_#    Short_Description

Try this : 尝试这个 :

            string[] inputs = {
                                  "#Title# and #Content#",
                                  "Product #TemplateName# #_Full_Product_Name_# more text. text text #_Short_Description_#"
                              };

            string pattern = "(?'string'#[^#]+#)";

            foreach (string input in inputs)
            {
                MatchCollection matches = Regex.Matches(input, pattern);
                Console.WriteLine(string.Join(",",matches.Cast<Match>().Select(x => x.Groups["string"].Value).ToArray()));
            }
            Console.ReadLine();

You regular expression is not correct. 您的正则表达式不正确。 In addition, you want to loop through match if you want all matching. 另外,如果要所有匹配,则要遍历匹配。

static void Main(string[] args)
{
    string input = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#",
        pattern = "#_[a-zA-Z_]*_#";
    Match match = Regex.Match(input, pattern);
    while (match.Success)
    {
        Console.WriteLine(match.Value);
        match = match.NextMatch();
    }
    Console.ReadLine();
}

Result 结果

在此处输入图片说明

Don't use anchors and change your regex to: 不要使用锚并将正则表达式更改为:

(#[^#]+#)

In regex the [^#] expression means any character BUT # 在正则表达式中, [^#]表达式表示任何字符,但#

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(#[^#]+#)";
      Regex rgx = new Regex(pattern);
      string sentence = "#blah blah# asdfasdfaf #somethingelse#";

      foreach (Match match in rgx.Matches(sentence))
     Console.WriteLine("Found '{0}' at position {1}", 
               match.Value, match.Index);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM