英文:
Regex for extractig dots
问题
I almost lost 4 hours triying to make it work and i give up.
给定以下字符串:
adsdasd.sdfdsfsdf.f43fvev.ASCSDF.sfgsdfssdf.yaml
我想要一个正则表达式,尽可能满足以下条件:
- 将所有的
.
替换为/
- 除了
.yaml
,它应该保持不变。
所以,最终字符串应该是:
adsdasd/sdfdsfsdf/f43fvev/ASCSDF/sfgsdfssdf.yaml
我尝试过:
- https://stackoverflow.com/questions/48365995/regex-to-match-one-character-exactly-one-time-anywhere-inside-a-string
- https://stackoverflow.com/questions/7124778/how-can-i-match-anything-up-until-this-sequence-of-characters-in-a-regular-exp
- 以及 regex101
英文:
I almost lost 4 hours triying to make it work and i give up.
Given the next string:
adsdasd.sdfdsfsdf.f43fvev.ASCSDF.sfgsdfssdf.yaml
I want a regex, posix possible to:
- substitute all the
.
for/
- except the
.yaml
that it should be remain so.
So, this must be the final string:
adsdasd/sdfdsfsdf/f43fvev/ASCSDF/sfgsdfssdf.yaml
I tried with:
答案1
得分: 2
这将帮助获取除最后一个点之外的所有点:
\.(?=[^.]*\.)
使用前瞻来检查我们找到的点后面是否有另一个点(前瞻不是匹配的一部分)。
英文:
This will help to get all the dots except the last one :
\.(?=[^.]*\.)
Using a lookahead to check that's there another dot after the one we found (the lookahead's not part of the match).
答案2
得分: 1
不确定这是否是您想要的。 https://regex101.com/r/3FpEbq/1
在这种情况下,您可以首先将所有的“.”替换为“/”,然后将“/yaml$”替换为“.yaml”。
英文:
Not sure if this is what you wanted. https://regex101.com/r/3FpEbq/1
Also in such situation you could replace first all the "." to "/", and then replace "/yaml$" to ".yaml"
答案3
得分: 1
.(?!yaml)
示例:
https://regex101.com/r/OdbsUb/1
英文:
You need negative lookahead (see https://www.rexegg.com/regex-lookarounds.html):
\.(?!yaml)
Example:
https://regex101.com/r/OdbsUb/1
答案4
得分: 1
正向预查就像 @SelVazi 一样,或者负向预查如下:
(?!\.[^.]*$)\.
请查看 https://regex101.com/r/lcd0ds/1 和 https://www.pcre.org/pcre.txt。
英文:
Generalized expression (means without "yaml" im regex):
positive lookahead like @SelVazi, or negative lookahead below:
(?!\.[^.]*$)\.
Look at https://regex101.com/r/lcd0ds/1 and https://www.pcre.org/pcre.txt
答案5
得分: 0
First thanks to all of you.
But it doesnt work and it wont work.
https://github.com/hashicorp/terraform/issues/28846
So i endend doing something like this, which TBH is a piece of crap...but...it opens another question which i will open here because i am again...lost.
> regex("/([^/]+)/?$", "fgdfsdf/sfsdfsdfsd/dsfsdfsdfs/gff0fff-fff-ff-dsfssd-cscsd.yaml")
[
"gff0fff-fff-ff-dsfssd-cscsd.yaml",
and wrap it into this horrible oneliner
[for k, v in nonsensitive(data.sops_file.account_secrets.data) : "path=${regex("/([^/]+)/?$", replace(replace(k, ".", "/"), "/yaml", ".yaml"))} | data = ${v}"]
Yes, this is for importing all secrets from a SOPS file, to vault using several foreach.
英文:
First thanks to all of you.
But it doesnt work and it wont work.
https://github.com/hashicorp/terraform/issues/28846
So i endend doing something like this, which TBH is a piece of crap...but...it opens another question which i will open here because i am again...lost.
> regex("/([^/]+)/?$", "fgdfsdf/sfsdfsdfsd/dsfsdfsdfs/gff0fff-fff-ff-dsfssd-cscsd.yaml")
[
"gff0fff-fff-ff-dsfssd-cscsd.yaml",
and wrap it into this horrible oneliner
[for k, v in nonsensitive(data.sops_file.account_secrets.data) : "path=${regex("/([^/]+)/?$", replace(replace(k, ".", "/"), "/yaml", ".yaml"))} | data = ${v}"]
Yes, this is for importing all secrets from a SOPS file, to vault using several foreach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论