Skip to content

Regex for Url Extraction — Pattern, Examples & Caveats

Pattern flags: gi
https?:\/\/[^\s<>\"']+

This pattern extracts HTTP and HTTPS URLs from surrounding text, matching the scheme and then any run of characters that are not whitespace or common delimiter quotes. It is used to pull links out of prose, logs or HTML. Extraction patterns pull substrings out of larger text rather than judging the whole input. They usually omit anchors and use the global flag to find every occurrence, often with capturing groups to isolate the part you want. Typical uses are scraping emails, URLs, tags or mentions from prose, logs and documents, where the surrounding text is irrelevant noise. It uses the gi flag(s). Apply it across text to collect every occurrence. It may swallow trailing punctuation (a period or comma at the end of a sentence); trim results for clean URLs. It excludes quotes and angle brackets so it stops at HTML attribute boundaries, but complex escaping can still confuse it. Only http/https are matched; extend the scheme alternation for other protocols if needed. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • https?:\/\/ — http:// or https:// scheme
  • [^\s<>\"']+ — URL characters excluding whitespace, angle brackets and quotes
  • g flag — find every URL
  • i flag — case-insensitive scheme matching

What it matches

see https://example.com/a?b=1 nowhttp://x.y and https://z.wlink: https://a.b/c

What it does not match

ftp://example.comexample.comno url here

When to use it

Reach for an extraction pattern when you need to pull structured tokens out of unstructured text: scraping contact details from a page, collecting links from a log, or harvesting tags from social content. Use capturing groups to isolate the part you want and the global flag to gather every occurrence. Clean the results afterward — extraction often grabs trailing punctuation or partial matches, so trim and deduplicate before storing or displaying the collected values.

Caveats

  • It may swallow trailing punctuation (a period or comma at the end of a sentence); trim results for clean URLs.
  • It excludes quotes and angle brackets so it stops at HTML attribute boundaries, but complex escaping can still confuse it.
  • Only http/https are matched; extend the scheme alternation for other protocols if needed.

Code examples

JavaScript
const re = /https?:\\/\\/[^\s<>\"']+/gi;
const text = "see see https://example.com/a?b=1 now and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence
Python
import re

rx = re.compile(r"https?:\/\/[^\s<>\"']+", re.I)
text = "see see https://example.com/a?b=1 now and more"
found = rx.findall(text)   # every occurrence (groups if present)
Java
import java.util.regex.*;

Pattern p = Pattern.compile("https?:\\/\\/[^\\s<>\\\"']+", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("see https://example.com/a?b=1 now");
boolean ok = m.find();   // scan for occurrences
PHP
<?php
$subject = "see https://example.com/a?b=1 now";
if (preg_match_all('/https?:\\/\\/[^\s<>\"']+/i', $subject, $m)) {
    // $m[0] is the full match; $m holds every occurrence
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

Why exclude < > and quotes?

When extracting from HTML or quoted text, those characters mark boundaries, so excluding them prevents the match from running into surrounding markup or string delimiters.

How do I clean trailing punctuation?

Strip trailing characters like . , ; : ) from each match, or use a more precise URL pattern; the greedy class often includes a sentence-final period.

Why do extraction patterns use the global flag?

Without g (or findall/preg_match_all), only the first occurrence is returned. The global flag iterates every match in the text, which is what you want when pulling all emails, URLs or tags from a document.

How do I capture just the part I want?

Wrap the target in a capturing group ( ... ) and read group 1 from each match. Use non-capturing groups (?: ... ) for structure you do not need, keeping the group numbering clean and predictable.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →