【发布时间】:2016-06-03 19:24:20
【问题描述】:
我正在文本中搜索在行中使用“variable2”且前面没有分号的行。这是我的正则表达式来解决这个问题。
^[^;]*?variable2
我对此的理解是,它应该找到以换行符开头的文本,以最小化非分号字符的数量,然后是“variable2”。 这无法选择我在此示例中所期望的内容。
Label0: mov variable0,WREG ;Some comment
mov W0,variable1
Label1: btsc variable2,#1 ;Some other comment
bra label2
我希望得到这个
Label1: btsc variable2
但选择了这个
mov W0,variable1
Label1: btsc variable2
我误会了什么?在我看来,非贪婪的表达并没有按照我的意图去做。如果我将正则表达式更改为^[^;\n]*?variable2,它会选择我期望它选择的内容。我将 Sublime Text 2 用于我的正则表达式,但我似乎在 php、javascript 和 python 中得到了相同的结果(根据 regex101.com)。
【问题讨论】:
标签: regex non-greedy