RegexCraft

URL regex

Matches http and https links in running text. Group 1 is the scheme, group 2 the host, group 3 an optional port and group 4 the path, which may not end in a full stop, comma or closing bracket, so a link at the end of a sentence comes out clean.

Open in the tester

The pattern

/(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?/gi

Sample text, 3 matches

Docs live at https://regex-craft.example.io/docs, the status page at http://status.internal:8080/healthz, and the raw spec at https://cdn.example.net/assets/schema-v2.json.

Point at a coloured group to see it in the pattern, the text and the explanation.

What it deliberately doesn’t do

  • The host needs at least one dot, so http://localhost is not matched.
  • Other schemes (ftp, mailto, file) are ignored on purpose.
  • It finds links; it does not validate them. Use the URL constructor for that.

Explained, in order

  1. (https?)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later reference
  2. httpMatches the text "http" literally
  3. sMatches the character 's' literally
  4. ?The preceding element matches optionally (zero or one time) (greedy)
  5. ://Matches the text "://" literally
  6. ([\w-]+(?:\.[\w-]+)+)Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference
  7. [\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'
  8. +The preceding element matches one or more times (greedy)
  9. (?:\.[\w-]+)Non-capturing group — groups the pattern without capturing
  10. \.Matches the character '.' literally
  11. [\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'
  12. +The preceding element matches one or more times (greedy)
  13. +The preceding element matches one or more times (greedy)
  14. (?::(\d+))Non-capturing group — groups the pattern without capturing
  15. :Matches the character ':' literally
  16. (\d+)Group 3.Capturing group #3 — groups the pattern and captures the matched text for later reference
  17. \dMatches a digit (0–9)
  18. +The preceding element matches one or more times (greedy)
  19. ?The preceding element matches optionally (zero or one time) (greedy)
  20. (/[^\s]*[^\s.,;:!?)\]])Group 4.Capturing group #4 — groups the pattern and captures the matched text for later reference
  21. /Matches the character '/' literally
  22. [^\s]Character class — matches any single character NOT in: a whitespace character (space, tab, newline, etc.)
  23. *The preceding element matches zero or more times (greedy)
  24. [^\s.,;:!?)\]]Character class — matches any single character NOT in: a whitespace character (space, tab, newline, etc.), '.', ',', ';', ':', '!', '?', ')', ']'
  25. ?The preceding element matches optionally (zero or one time) (greedy)

In your language

JavaScript

The engine this page was tested with.

const re = /(https?):\/\/([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(\/[^\s]*[^\s.,;:!?)\]])?/gi;

// Example
const matches = [...str.matchAll(/(https?):\/\/([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(\/[^\s]*[^\s.,;:!?)\]])?/gi)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

pattern = re.compile(r"(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?", re.IGNORECASE)

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

// Note: Inline flags at the start of the pattern: (?i), (?m), (?s).

var re = regexp.MustCompile(`(?i)(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).

fn main() {
    let re = Regex::new(r#"(?i)(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}