Skip to content

KHAPCHI.COM

One stick to rule all staffs!

  • Home
  • Technology
  • Tools
  • About
  • Toggle search form
  • 7 Commands to know your Linux Server Technology
  • Install and configure Squid3 for Ubuntu20.04 for known hosts – IP Access Uncategorized
  • How to store JSON response from an API to MySQL table? Technology
  • How to make DNS lookup of an IP or Domain using Python3. Technology
  • How to connect to Local MySQL Db using Python3 Technology
  • Begin! Uncategorized
  • How to fetch all DNS records of a Domain using Python3. Technology
  • Store JSON response from API as separate columns in MySQL table? Technology

Python3 regular expression to fetch Domains from a string or Paragraph.

Posted on December 20, 2022December 20, 2022 By Khapchi No Comments on Python3 regular expression to fetch Domains from a string or Paragraph.

To generate a regular expression that can extract valid domain names without “www” from a paragraph, you can use the following pattern:

(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]

This regular expression uses the following elements:

  • (?:...) is a non-capturing group, which allows you to group parts of the pattern without creating a capturing group.
  • [a-z0-9] matches any lowercase letter or digit.
  • [a-z0-9-]{0,61} matches any lowercase letter, digit, or hyphen, occurring 0 to 61 times.
  • + is a quantifier that matches the preceding element one or more times.
  • \. matches a literal dot.

This regular expression will match domain names that consist of one or more subdomains, followed by a top-level domain (TLD). For example, it will match “example.com”, “subdomain.example.com”, and “sub.subdomain.example.com”, but it will not match “www.example.com“.

To use this regular expression to extract domain names from a paragraph, you can use a regular expression library in your programming language of choice. For example, in Python you can use the re module:

import re

pattern = r"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"

text = "The website is located at example.com. Please visit us at subdomain.example.com."

matches = re.findall(pattern, text)

print(matches)  # Output: ["example.com", "subdomain.example.com"]

I hope this helps! Let me know if you have any questions.

Technology

Post navigation

Previous Post: Store JSON response from API as separate columns in MySQL table?

Related Posts

  • How to fetch all DNS records of a Domain using Python3. Technology
  • Multilib problems while using yum. Technology
  • Prepare a Linux server for first use. Technology
  • How to connect to Local MySQL Db using Python3 Technology
  • Store JSON response from API as separate columns in MySQL table? Technology
  • How to Query any REST API using Python3 easy. Technology

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Recent Posts

  • Python3 regular expression to fetch Domains from a string or Paragraph.
  • Store JSON response from API as separate columns in MySQL table?
  • How to store JSON response from an API to MySQL table?
  • How to Query any REST API using Python3 easy.
  • How to fetch all DNS records of a Domain using Python3.

Recent Comments

    Archives

    • December 2022
    • July 2020
    • May 2020

    Categories

    • Technology
    • Uncategorized

    Archives

    • December 2022
    • July 2020
    • May 2020
    • Multilib problems while using yum. Technology
    • How to store JSON response from an API to MySQL table? Technology
    • How to make DNS lookup of an IP or Domain using Python3. Technology
    • How to connect to Local MySQL Db using Python3 Technology
    • How to fetch all DNS records of a Domain using Python3. Technology
    • Begin! Uncategorized
    • Store JSON response from API as separate columns in MySQL table? Technology
    • 7 Commands to know your Linux Server Technology

    Copyright © 2025 KHAPCHI.COM.

    Powered by PressBook News WordPress theme