How to Extract URLs from Text Using Regex?
Extracting URLs from text can be challenging, but with the right regex pattern, you can easily find and list them all.
Formula Genius can generate a draft from a plain-English prompt. Supported formulas can be checked with sample cases; review the coverage on the result.
The formula
"Extract all URLs from a block of text, including http and https links"
(https?://[\w.-]+(?:\.[\w.-]+)+[/\w.-]*)
This regex pattern captures all URLs starting with http or https.
Step-by-step breakdown
- The pattern starts with 'https?://' to match both 'http://' and 'https://'.
- Next, '[\w.-]+' matches the domain name, allowing letters, numbers, dots, and hyphens.
- The '(?:\.[\w.-]+)+' part captures the top-level domain (like .com, .org).
- Finally, '[/\w.-]*' matches any additional path or parameters in the URL.
Edge cases & warnings
- URLs without a scheme (http/https) won't be captured.
- URLs with unusual characters may not be matched correctly.
- Long URLs that span multiple lines may be partially matched.
- URLs embedded in HTML tags may require additional handling.
Examples
"Check out https://www.example.com and http://test.com for more info."
https://www.example.com, http://test.com
"Visit our site at http://my-site.org or https://secure-site.net."
http://my-site.org, https://secure-site.net
Frequently asked questions
Can this regex extract URLs from HTML?
No, this regex is designed for plain text and may not handle HTML tags.
What if the URL contains query parameters?
The regex will capture URLs with query parameters as long as they follow the specified format.
Is this regex case-sensitive?
By default, regex is case-sensitive; however, you can add a case-insensitive flag to modify this behavior.
Can't find what you need?
Describe a formula in plain English. Formula Genius drafts and explains it, and can check supported formulas on sample cases.