2

Golang - extract links using regex

I need to get all links from text which are in specific domain example.de using Regex in Go

Below are all possible links that should be extracted:

https://example.de 
https://example.de/
https://example.de/home
https://example.de/home/
https://example.de/home some text that should not be extracted
https://abc.example.de
https://abc.example.de/
https://abc.example.de/home
https://abc.example.de/home
https://abc.example.de/home some text that should not be extracted

What I already tried

I used this website to check if my regex are correct: https://regex101.com/r/ohxUcG/2 and here are combinations that failed:

  • https?://*.+example.de*.+ failed on expression https://abc.example.de/a1b2c3 dsadsa getting whole text to the \n instead of https://abc.example.de/a1b2c3 without dsadsa
  • https?://*.+example.de*.+\s(\w+)$ this gets links that are terminated only with space but sometimes links can be terminated with \n or \t etc.

Resources which may be useful

1
  • 1
    Do you mean these strings are part of a longer text? Try (?:https?://)?(?:[^/.]+\.)*\bexample\.de(?:/[^/\s]+)*/?. See regex101.com/r/ohxUcG/6 Commented May 13, 2022 at 9:28

1 Answer 1

3

You can use

(?:https?://)?(?:[^/.]+\.)*\bexample\.de\b(?:/[^/\s]+)*/?

See the regex demo. Details:

  • (?:https?://)? - an optional http:// or https:// string
  • (?:[^/.]+\.)* - zero or more sequences of one or more chars other than a / and . chars and then a . char
  • \bexample\.de\b - a whole word example.de
  • (?:/[^/\s]+)* - zero or more repetitions of / and then one or more chars other than whitespace and /
  • /? - an optional / char.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, I have one last question how this regex will look like if there would be more domains like example.nl or example.fr not only example.de, thanks in advance!
@mikolajsemeniuk Replace \bexample\.de\b with \bexample\.(?:de|fr|nl|co\.uk|etc.)\b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.