Logo Wand.Tools

Reguläre Ausdrücke Generator

Intelligente Generierung und Erklärung von regulären Ausdrücken, unterstützt verschiedene gängige Mustervergleiche

Wie man reguläre Ausdrücke verwendet, um URLs abzugleichen

✅ Gängige URL-Formate

  • http://www.example.com
  • https://example.com/path?query=123
  • ftp://ftp.example.org/resource.txt
  • www.example.com
  • example.com

📌 Kernsyntax für reguläre Ausdrücke (für URLs)

Der folgende reguläre Ausdruck kann die meisten Standard-URLs abgleichen:

\b((?:https?|ftp):\/\/)?(?:www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?

🧪 Python Beispiel: URLs aus Text extrahieren

import re

text = """
Visit our site at https://www.example.com or follow the docs at http://docs.example.org/page.
Also check ftp://ftp.example.com/file and plain www.test.com or example.net for more.
"""

pattern = r'\b((?:https?|ftp):\/\/)?(?:www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?'
urls = re.findall(pattern, text)
print(urls)

🌐 JavaScript Beispiel: Alle Links von einer Webseite extrahieren

const text = `
  Check https://www.example.com, http://example.org, ftp://files.example.net,
  and also www.test.com or just example.co.
`;

const regex = /\b((?:https?|ftp):\/\/)?(?:www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?/g;
const urls = text.match(regex);
console.log(urls);

☕ Java Beispiel: Verwenden von Pattern zum Abgleichen von URLs

import java.util.regex.*;
import java.util.*;

public class URLMatcher {
    public static void main(String[] args) {
        String text = "Visit https://example.com or ftp://ftp.example.org.";
        String regex = "\\b((?:https?|ftp):\\/\\/)?(?:www\\.)?[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(?:\\/[^\\s]*)?";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

🔎 Fortgeschrittene Optimierung: Ein leistungsfähigerer Regex zum Abgleichen von URLs

\b((?:https?|ftp):\/\/)?(?:www\.)?[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2,})(?::\d{1,5})?(?:\/[^\s]*)?

Dieser Regex unterstützt:

  • Optionales Protokoll (http/https/ftp)
  • Optionales www
  • Top-Level-Domain (.com/.org/.net/.io usw.)
  • Optionale Portnummer (:80, :443 usw.)
  • Optionaler Pfad und Parameter (/path?query)

🔍 Praktische Szenarien

  • Extrahieren aller Linkadressen aus Webseiten-HTML
  • Erfassung von URLs in Logdateien
  • Extrahieren von Hyperlinks aus Chatverläufen, Dokumentinhalten
  • Extrahieren von Eingaben für benutzerdefinierte Suchmaschinen