【问题标题】:Match everything before a specific word in a multiline string匹配多行字符串中特定单词之前的所有内容
【发布时间】:2012-01-24 14:04:30
【问题描述】:

我正在尝试使用正则表达式从字符串中过滤掉一些垃圾文本,但似乎无法使其正常工作。我不是正则表达式专家(甚至不是很接近),我已经搜索了类似的示例,但似乎没有一个可以解决我的问题。

我需要一个正则表达式,它匹配从字符串开头到该字符串中特定单词的所有内容,但不匹配单词本身。

这是一个例子:

<p>This is the string I want to process with as you can see also contains HTML tags like <i>this</i> and <strong>this</strong></p>
<p>I want to remove everything in the string BEFORE the word "giraffe" (but not "giraffe" itself and keep everything after it.</p>

那么,如何匹配字符串中“giraffe”一词之前的所有内容?

谢谢!

【问题讨论】:

    标签: c# regex


    【解决方案1】:
    resultString = Regex.Replace(subjectString, 
        @"\A             # Start of string
        (?:              # Match...
         (?!""giraffe"") #  (unless we're at the start of the string ""giraffe"")
        .                #  any character (including newlines)
        )*               # zero or more times", 
        "", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
    

    应该可以。

    【讨论】:

      【解决方案2】:

      为什么是正则表达式?

      String s = "blagiraffe";
      s = s.SubString(s.IndexOf("giraffe"));
      

      【讨论】:

        【解决方案3】:

        试试这个:

            var s =
                 @"<p>This is the string I want to process with as you can see also contains HTML tags like <i>this</i> and <strong>this</strong></p>
                 <p>I want to remove everything in the string BEFORE the word ""giraffe"" (but not ""giraffe"" itself and keep everything after it.</p>";
            var ex = new Regex("giraffe.*$", RegexOptions.Multiline);
            Console.WriteLine(ex.Match(s).Value);
        

        这段代码 sn-p 产生以下输出:

        giraffe" (but not "giraffe" itself and keep everything after it.</p>
        

        【讨论】:

          【解决方案4】:

          look-ahead 可以解决问题:

          ^.*(?=\s+giraffe)
          

          【讨论】:

            【解决方案5】:

            你可以使用这样的前瞻模式

            ^.*?(?=giraffe)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-12-22
              • 1970-01-01
              • 1970-01-01
              • 2018-11-25
              • 1970-01-01
              • 2011-03-12
              • 2011-01-25
              相关资源
              最近更新 更多