C#中的正则表达式

时间:2012-07-02 20:57:06

标签: c# regex

  

可能重复:
  Using C# regular expressions to remove HTML tags
  Regex Pattern in C#

我有这样的输入如何将其转换为C#

Input = <!--EVENT-GALLERY-VIEW WIDTH=500 --> 
Output = "<widget:EventList id=\"EventList1\" Width=\"500\" runat=\"server\" />"

Input = <!--EVENT-GALLERY-VIEW WIDTH=500 CATEGORY=SPORTS --> 
Output = <widget:EventList id=\"EventList1\" Width=\"500\" runat=\"server\" Category=\"Sport\" />"

Follwing代码适用于第一种情况但不适用于第二种情况如何更改var pattern = @“(\ w *)(\ s *))*(\ s *)( - &gt;)”

static void Main(string[] args)
        {
            var result = "<!--EVENT-GALLERY-VIEW WIDTH=500 -->";
            var pattern = @"(<!--)(\s*)(EVENT-GALLERY-VIEW)(\s*)((WIDTH)(=)(?<value>\w*)(\s*))*(\s*)(-->)|(<!--)(\s*)(EVENT-GALLERY-VIEW)(\s*)((WIDTH)(=)(?<value>\w*)(\s*))*(\s*)(-->)";
            var replaceTag = "<widget:EventList id=\"EventList@@id\" Width=\"@@value\" runat=\"server\" />";

            result = RegexReplaceWithUniqueTag(result, pattern, replaceTag);
        }

        static string RegexReplaceWithUniqueTag(string result, string pattern, string replaceTag)
        {
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(result);
            for (int i = mc.Count - 1; i >= 0; i--)
            {
                string newreplaceTag = replaceTag;
                newreplaceTag = newreplaceTag.Replace("@@id", i.ToString(CultureInfo.InvariantCulture));
                if (mc[i].Groups["value"] != null)
                    newreplaceTag = newreplaceTag.Replace("@@value", mc[i].Groups["value"].Value);
                result = result.Remove(mc[i].Index, mc[i].Length);
                result = result.Insert(mc[i].Index, newreplaceTag);
            }
            return result;
        }

1 个答案:

答案 0 :(得分:2)

您可以使用?(0或1)运算符将语句标记为可选,如下所示:

(CATEGORY=(?<category>\w*))?

这将找到0或1次CATEGORY=[WORD]

您可能会觉得有用的其他一些正则表达式运算符是:

+(1或更多)
*(0或更多)

您可以找到有关正则表达式字符herehere的更多信息。