簡體   English   中英

VB.Net-正則表達式問題

[英]VB.Net - Regex Issue

我嘗試制作一個正則表達式模式,該模式可以識別[====] [====]之間有多少文本都沒有關系,如果之間有新行,也應該匹配它。 但是它應該始終與下一個事件匹配,這意味着如果我有這樣的事情。

[== Cats and Dogs ==]  --> Match

要么

[== fsdfd
sdfsdf

sdfsdf ==]             --> Match

[== ddasdas [== asdsd ==]  --> Match 

從第一個[==到最后一個==] 如果沒有右括號,則需要忽略第二個[==

我怎樣才能做到這一點?

共有三種可能的方法,但由於您的要求是相互排斥的,因此在所有情況下都無法使用。

  1. 與問題中所有情況匹配的解決方案(假設您的意思是==]您寫的位置=] ;我“修復”了這些情況):

     Dim RegexObj As New Regex("\\[==.*?==\\]", RegexOptions.Singleline) 

    從最左邊的[==直到第一個==]匹配。 它將無法正確處理嵌套的括號,就像我在上面的評論中,結果將是[== foo [== bar ==]

  2. 該解決方案可匹配所有情況,包括我的評論中的情況,但僅在整個輸入字符串中可能存在一個完全匹配的情況下才有效:

     Dim RegexObj As New Regex("\\[==.*==\\]", RegexOptions.Singleline) 

    這從找到的最左邊的[==到最后一個==]匹配。

  3. 該手柄正確的嵌套的情況,但是該解決方案在不正確嵌套例失敗(例如,只匹配[== asdsd ==]當施加到[== ddasdas [== asdsd ==]

     Dim RegexObj As New Regex( _ "\\[== # Match [==" & chr(10) & _ "(?> # Then either match (possessively):" & chr(10) & _ " (?: # the following group which matches" & chr(10) & _ " (?!\\[==|==\\]) # only if we're not before a [== or ==]" & chr(10) & _ " . # any character" & chr(10) & _ " )+ # once or more" & chr(10) & _ "| # or" & chr(10) & _ " \\[== (?<Depth>) # [== (and increase the nesting counter)" & chr(10) & _ "| # or" & chr(10) & _ " ==\\] (?<-Depth>) # ==] (and decrease the nesting counter)." & chr(10) & _ ")* # Repeat as needed." & chr(10) & _ "(?(Depth)(?!)) # Assert that the nesting counter is at zero." & chr(10) & _ "==\\] # Then match ==].", _ RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline) 

選一個 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM