[英]How to extract a quoted string using regular expression? OR How to use this particular regular expression (“[^”]*\.csv") in c#?
我試圖從HTML響應中提取引用的文本。 但是,當我使用正則表達式匹配數據時,我收到錯誤消息“無法識別的轉義序列”。 代碼粘貼在下面。 我錯過了一些基本的東西嗎?
static private string ParseText2GetLink(string response)
{
Match match = Regex.Match( response, @"("[^"]*\.csv")" );
string key = null;
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
key = match.Groups[1].Value;
Console.WriteLine(key);
}
return key;
}
要在@""
字符串中包含雙引號,您必須將其加倍:
@"(""[^""]*\.csv"")"
或者您可以使用普通字符串:
"(\"[^\"]*\\.csv\")"
(注意,現在你必須逃避反斜杠。)
你的問題是,字符串中的\\是一個轉義序列。 你必須逃脫\\所以它成為
("[^"]*\\.csv")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.