0

I'm pretty sure it should be simple enough to do but I cannot figure out. I have some FQDNs like:

  1. api.mydiomain.org.local
  2. app.stg.mydiomain.org.local
  3. uses.anotherdomain.uk

and I like to extract mydiomain.org.local, stg.mydiomain.org.local, anotherdomain.uk etc. respectively using perl style regex. I tried [\\W].+$, which is giving me almost what I want but with the leading . (e.g. .mydiomain.org.local). And [^\\W]+$ returning only the very last part (e.g. local). Any idea what am I missing here?

Just to mention that I'll be using this with terraform's regex() function, which is fully compatible with perl RegEx but if the pattern has no capture groups it returns a string but behaves differently for named and unnamed capture group(s), returning a map{} and list() instead.

MacUsers
  • 2,091
  • 3
  • 35
  • 56
  • 1
    See [zero-width positive lookbehind assertion](https://perldoc.perl.org/perlre#(?pattern)) in perlre, i.e. `(?<=\.)[\w.]+$` – Steffen Ullrich Jul 08 '23 at 21:08
  • try `^[^.]*\.\K.+$` https://regex101.com/r/4u43kl/1 – sln Jul 08 '23 at 21:45
  • 1
    Regex is not really necessary here. How about splitting the string by `.`, throw the first and join the rest? – InSync Jul 08 '23 at 22:54
  • 1
    You should update your question to note that you are using terraform so someone can suggest an appropriate terraform solution. – brian d foy Jul 09 '23 at 16:26
  • Does this answer your question? [How to get domain name from URL](https://stackoverflow.com/questions/569137/how-to-get-domain-name-from-url) – ti7 Jul 09 '23 at 17:29

1 Answers1

1

Hello try this regex ^\w+\.(.*)

  • Thanks @jean for your replay! It's working but returning a _list_ (using it in Terraform) for me, in stead of string. I can do something like `regex("^\w+.(.+)", "api.mydiomain.org.local")[0]` to get the result as string but was wondering is there any way a string can be returned without any further processing? – MacUsers Jul 08 '23 at 22:23
  • @MacUsers You can use `$1`. No "further processing" needed. Well, if you were actually using Perl as you stated. What you have there isn't Perl. – ikegami Jul 09 '23 at 00:12