![](/img/trans.png)
[英]regex how to extract strings between delimiters and not double delimiters
[英]regex extract value from the string between delimiters
我有一個大字符串,我需要從中提取字符串值。 字符串值位於定界符之間
category = '
和
';
這是我的正則表達式,但是我需要避免輸出定界符。
String productCategory = Regex.Match(html, @"category = '(.*?)';").Value;
這是示例category = 'Video Cards';
我需要提取Video Cards
可以使用的是先行和后行運算符,因此最終會得到如下結果:
string pattern = @"(?<=category = ').*(?=';)";
string productCategory = Regex.Match(html, pattern ).Value;
還值得一提的是, 用正則表達式解析HTML是一個壞主意 。 您應該使用HTML解析器來解析HTML。
您是否考慮過使用MatchObj.Groups
屬性? 如果您在Derek Slager's之類的測試站點上測試當前的正則表達式,您會確切地注意到您想要的是第一個Group。 您應該只能夠調用第一個組並獲得所需的內容。
productCategory.Groups[0].Value
您要提取組:
String productCategory = Regex.Match(html, @"category = '(.*?)';").Groups[1].Value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.