雅努什,這些簡單的正則表達式會做你想要什麼
對於第一個:搜索
(?is)<nav class="filters">[^<]*</nav>
對於第二之一:搜索
(?is)<!-- Google Tag Manager -->[^<]*<!-- End Google Tag Manager -->
實施例:
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
string resultString = null;
string s1 = @"string was here>>> <nav class=""filters"">
STRING CONTENT
</nav> <<< string was here";
string s2 = Regex.Replace(s1, "(?is)<nav class=\"filters\">[^<]*</nav>", "");
Console.WriteLine(s2);
Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();
} // END Main
} // END Program
輸出:
string was here>>> <<< string was here
[^<]*
會吃掉任何不是<
的字符,包括換行符,這是您的問題。
有了您的.*
點星級語法,你也可以吃換行,但只能通過設置(?s)
「點相匹配換行」標誌。但總的來說,只要點星可以通過避免一些更具體的,我們這樣做的[^<]*
因此選擇請注意,在""filters""
雙雙引號什麼都沒有做的正則表達式。你可能知道這一點,但兩個雙引號是你如何在逐字字符串中避免雙引號。
什麼不工作? –
呃...我的正則表達式來找到我需要的東西 – pee2pee
你想捕捉什麼?並用什麼取代? – sshashank124