簡體   English   中英

匹配除 ' 之外的任何字符

[英]Match any character except '

我想匹配任何字符(不區分大小寫),除非前面是單引號,后跟文本 On Error Goto:

比賽:

on error goto err_handler
if aap = 0 then on error goto Myerrorhandler
    on error goto errorhandler1
   on error goto errorhandler2

不匹配:

' on error goto errorhandler3
'   if aap =0 then on error goto errorhandler4
Any line not containing On Error Goto

我試過: [^']*(On Error Goto)但這沒有用。

測試程序中是否使用了Errorhandler

謝謝!

更新了正則表達式測試用例的鏈接: https : //regex101.com/r/UYll0h/6

由於'不存在時不能有任何字符,因此您需要使用前瞻斷言。

由於on error goto之前的行中也可能有代碼的其他字符(除了' )(就像在行if aap = 0 then on error goto Myerrorhandler ),要處理這些if aap = 0 then on error goto Myerrorhandler ,您還需要放置一個條件檢查前瞻后是否存在除'以外'任何字符。 這將由([^']+)? .

^(?!')([^']+)?on error goto

(?)稱為前瞻。 它檢查其中的字符是否存在。 []不同,即使沒有字符, (?)也會斷言為真。 例如, [a]將檢查第一個字符是否為 'a',但它之后的任何表達式將從第二個字符開始檢查。 另一方面, (?=a)將檢查第一個字符是否為 'a',並且它之后的任何表達式將從第一個字符開始檢查。 換句話說,如果未找到匹配項,前瞻不會將正則表達式引擎移動到下一個字符。

^[^'\n\r]*On Error Goto

使用i不區分大小寫模式和m多行模式。 證明

解釋

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [^'\n\r]*                any character except: ''', '\n' (newline),
                           '\r' (carriage return) (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  On Error Goto            'On Error Goto'

暫無
暫無

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

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