正则表达式与 ?被动群内的量词?
我正在编辑 SQL 的 TextMate 语法。它目前有正则表达式(为了澄清而省略了关键字):
(?i:^\s*(create)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
这正确匹配了一个函数定义,就像
CREATE FUNCTION similarity
我想处理CREATE OR REPLACE
一样,所以我将正则表达式更改为
(?i:^\s*(create(\s+or\s+replace)?)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
,它不会匹配CREATE
或CREATE OR REPLACE
。我也通过使新的可选组被动来修复它:
(?i:^\s*(create(?i:\s+or\s+replace)?)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
但是..为什么它之前不匹配?我希望它能够匹配,但随后可能会给我一个我没有预料到的捕获组(如果外部被动组指示器没有渗透到新的内部组)。
I'm editing a TextMate grammar for SQL. It currently has the regex (keywords omitted for clarify):
(?i:^\s*(create)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
This correctly matches a function definition like
CREATE FUNCTION similarity
I wanted to handle CREATE OR REPLACE
, so I changed the regex to
(?i:^\s*(create(\s+or\s+replace)?)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
and it wouldn't match a CREATE
or a CREATE OR REPLACE
. I fixed it by making the new optional group passive as well:
(?i:^\s*(create(?i:\s+or\s+replace)?)\s+(aggregate|function|(unique\s+)?index|table)\s+)(['"`]?)(\w+)\4
But.. why didn't it match before? I would expect it to match, but then to possibly give me a capture group I wasn't expecting (if the outer passive-group indicator doesn't trickle down to the new inner group).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你的 \4——当你第一次添加新的括号时,你需要将它更改为 \5。
It was your \4--you needed to change it to a \5 when you first added the new parens.