前瞻正则表达式替代方案
我正在使用 ElasticSearch 快照恢复模块并尝试使其正常工作:
"rename_pattern": "^foo_(.+)(_bar)?$",
"rename_replacement": "$1",
这意味着字符串必须以 foo_ 开头,后跟任意数量的字符,并且必须以 _bar 结尾,这是可选的。因此 foo_some_string_bar 和 foo_some_string 都必须以 some_string 结尾。 foo_some_string_bar_bar 必须以 some_string_bar 结尾。
这可能吗?
谢谢。
ps:我设法这样做:
$ echo foo_some_string | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string
$ echo foo_some_string_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string
$ echo foo_some_string_bar_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string_bar
$
它有效,但它太棘手和麻烦。
I'm using ElasticSearch snapshot restore module and trying to get this to work:
"rename_pattern": "^foo_(.+)(_bar)?quot;,
"rename_replacement": "$1",
Meaning the string must start with foo_, followed by any number of characters, and it must end with _bar, which is optional. So both foo_some_string_bar and foo_some_string must end up as some_string. foo_some_string_bar_bar must end up as some_string_bar.
Is this possible?
Thanks.
p.s: I managed to do it like this:
$ echo foo_some_string | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string
$ echo foo_some_string_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string
$ echo foo_some_string_bar_bar | sed -nE '/^foo_(((.+)_bar)|(.+))$/s//\3\4/p'
some_string_bar
$
It works, but it's sooo tricky and cumbersome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。替换这个:
与这个:
解决了问题。问题是 (.+)(_bar)?$ 无论如何都会贪婪地匹配所有内容。但尴尬|模式可以满足我的需要:要么匹配末尾 _bar 之前的所有内容,要么匹配整个剩余部分(如果字符串不以 _bar 结尾)。
Sure. Replacing this:
with this:
Solved the problem. The problem was that (.+)(_bar)?$ greedily matched everything no matter what. But the awkward | patterns do what I need: either match everything before _bar at the end, or just match the whole remainder if the string doesn't end with _bar.