Skip to content

Instantly share code, notes, and snippets.

@dizzersee
Last active October 21, 2019 17:10
Show Gist options
  • Save dizzersee/8457b22a28330f724409ff9e36ff4de6 to your computer and use it in GitHub Desktop.
Save dizzersee/8457b22a28330f724409ff9e36ff4de6 to your computer and use it in GitHub Desktop.
Regex that matches a URL until a specific sequence appears
https\:\/\/(www.)?(domain.com\/?)\S*?(?=((\<br)|(\(\()| |$))
Test https://www.regextester.com/
======== REGEX ========
\S*?
- ? is Lazy operator: Match as few characters as possible
(?=((\<br)|(\(\()| |$))
- Positive lookahead ?= matches a group after the main expression without including it in the result.
- 4 Groups here: <br, ((, space, $
======== EXAMPLES ========
- Match www/non-www URL until a "<br", "((", or a space
Lorem ipsum https://www.domain.com((lorem
- matches https://www.domain.com
Lorem ipsum https://www.domain.com/((ebig
- matches https://www.domain.com/
Lorem ipsum https://www.domain.com/lorem((lorem
- matches https://www.domain.com/lorem
Lorem ipsum https://www.domain.com/lorem((lorem
- matches https://www.domain.com/lorem
Lorem ipsum https://www.domain.com<br
- matches https://www.domain.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment