【发布时间】:2026-01-31 08:30:01
【问题描述】:
所以我在使用 Regex 时遇到了一些问题。我需要匹配所有出现的
--property:value --property:value ...
Slack Slash Commands 使用需要字符串操作的POSTing 数据的方法,因为它在命令/example --property:value --other:value 之后获取所有文本并将其发送为:
text: --property:value --other:value
我对正则表达式不是很厉害,但我想出了以下几点:
--([^:=]+)([=]|[:])([^-]+)
这样,它可以处理任何类似的事情
--property:value
--property:value with spaces
--property:value_with_underscores
--property=value
--property=value with spaces
--property=value_with_underscores
// etc etc
我确实需要在拆分后修剪值以避免命令之间出现额外的空白,但这很容易做到。
唯一失败的地方是:
--property:value-with-dashes
由于我捕获了 ([^-]+) 的 X 个实例,它看到第一个 - 并终止,所以我结束了
--property:value // Missing `-with-dashes`, as invalid Match
基本上,不是在第一个 - 处终止,我需要它在第一个 -- 处终止(即另一个命令的开始),但我不确定如何解决这个问题......我不确定术语(负面预测已经出现了几次,但我不确定如何让它发挥作用)
【问题讨论】:
标签: php regex pattern-matching slack