正则表达式提取点号的模式是:`\.`

huangapple go评论66阅读模式
英文:

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

我尝试过:

正则表达式提取点号的模式是:`\.`

英文:

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

这将帮助获取除最后一个点之外的所有点:

\.(?=[^.]*\.)

使用前瞻来检查我们找到的点后面是否有另一个点(前瞻不是匹配的一部分)。

Regex101链接

英文:

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).

Regex101

答案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/1https://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.

huangapple
  • 本文由 发表于 2023年3月10日 00:29:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定