0

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:

:(

5 Answers5

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

Regex101

SelVazi
  • 10,028
  • 2
  • 13
  • 29
1

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"

zolo
  • 444
  • 2
  • 6
1

You need negative lookahead (see https://www.rexegg.com/regex-lookarounds.html):

\.(?!yaml)

Example: https://regex101.com/r/OdbsUb/1

j.holetzeck
  • 4,048
  • 2
  • 21
  • 29
1

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

msegit
  • 110
  • 8
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.