[英]Multi Line Regex Issue
我一直在Stackoverflow和許多其他網站上搜索,但我似乎無法試圖讓正則表達式匹配我需要的東西。
請看下面的例子:
這是我正在搜索的文字。
[Date]
;Possible values: Local, Static
;Type = Local
;If using Static type, the year/month/date to set the date to
;Year = 2012
;Month = 1
;Date = 1
[Time]
;Possible values: Local, Custom, Static
Type = Static
;If using Custom type, offset from UTC in hours (can be negative as well)
Offset = 0
;If using Static type (Hour value always the same on every server start), the value (0-24) to set the Hour to
Hour = 9
我想要完成的是前瞻,只獲得[時間]括號下的Type = Static。 我正在使用C#,如果這有幫助。 我嘗試了許多不同的模式但沒有成功。
(?<=\[Time\]\n+Type = ).*
(?<=\[Time\].*Type =).*
這只是我嘗試過的一些想法。 有人可以告訴我這樣做的正確方法,解釋為什么它正在做它做的事情? 基於這些評論,我注意到我應該更清楚這個文件比我顯示的要大得多,而且幾乎每個[SETTING]都包含至少一個類型的標志。 此外,它幾乎100%確定用戶將擁有;注釋放入文件中,因此我必須能夠搜索特定的[設置]並鍵入以使其工作。
var val = Regex.Match(alltext,
@"\[Time\].+?Type\s+=\s+([^\s]+)",
RegexOptions.Singleline)
.Groups[1].Value;
這將返回靜態
您可以嘗試不同的模式:
(?<=Type\s*=\s*).*(?=[\r\n;]+)
我假設Type =
后面跟着一個以分號開頭的新行;
。
感謝@SimonWhitehead和@HamZa提醒我,我應該注意這將捕獲Type =
行,因此您需要忽略第一個匹配並僅查找第二個匹配。
編輯:您可以嘗試另一個表達式,它不如第一個表達式有效:
(?<=\[Time\][\r\n;]*[^\r\n]+[\r\n]*Type\s*=\s*).*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.