[英]Match everything before a specific word in a multiline string
我正在嘗試從帶有正則表達式的字符串中過濾掉一些垃圾文本但似乎無法使其工作。 我不是一個正則表達式專家(甚至不是很接近),我搜索了類似的例子,但似乎沒有解決我的問題。
我需要一個正則表達式匹配從字符串的開頭到該字符串中的特定單詞但不是單詞本身的所有內容。
這是一個例子:
<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>
那么,如何在“長頸鹿”這個詞之前匹配字符串中的所有內容?
謝謝!
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);
應該管用。
為什么正則表達式?
String s = "blagiraffe";
s = s.SubString(s.IndexOf("giraffe"));
嘗試這個:
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);
此代碼段生成以下輸出:
giraffe" (but not "giraffe" itself and keep everything after it.</p>
一個前瞻會做的伎倆:
^.*(?=\s+giraffe)
你可以使用像這樣的前瞻模式
^.*?(?=giraffe)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.