This comprehensive guide serves as a quick reference for developers to find and understand 5 popular regex patterns without scouring through Stack Overflow. From email and IP address patterns to fun facts about regex, discover everything you need to enhance your coding projects.
- What is Regex?
- Email Address Pattern
- IP Address Pattern
- URL Pattern
- Date Pattern
- Phone Number Pattern
- Extracting Values using Regex in Python and TypeScript
- Extra Tips and Resources
- Fun Facts about Regex
- Acknowledgements
Regex, or Regular Expressions, is a sequence of characters that forms a search pattern. Regex can be used for everything from looking for specific patterns in a block of text to replace one substring with another substring. It is widely used in programming for pattern matching with strings. Learn more about it in the official documentation and the RFC.
Matches most email address patterns adhering to the general structure of a local part, "@" symbol, and domain.
import re
pattern = re.compile(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')
matches = pattern.findall('[email protected]')
for match in matches:
print(match)
let pattern = /[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+/g;
let result = '[email protected]'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
let pattern = /[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+/g;
let result = '[email protected]'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+");
Matcher matcher = pattern.matcher("[email protected]");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Matches IP addresses, both IPv4 and IPv6.
import re
pattern = re.compile(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
matches = pattern.findall('192.168.1.1')
for match in matches:
print(match)
let pattern = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/g;
let result = '192.168.1.1'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
let pattern = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/g;
let result = '192.168.1.1'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b");
Matcher matcher = pattern.matcher("192.168.1.1");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Matches URLs, which can include http, https, www prefixes, and domains.
import re
pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
matches = pattern.findall('https://www.example.com')
for match in matches:
print(match)
let pattern = /http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+/g;
let result = 'https://www.example.com'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
let pattern = /http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+/g;
let result = 'https://www.example.com'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("http[s]?:\\/\\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+");
Matcher matcher = pattern.matcher("https://www.example.com");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Matches dates in various formats including dd/mm/yyyy, mm/dd/yyyy, and yyyy-mm-dd.
import re
pattern = re.compile(r'\b(0?[1-9]|[12]\d|3[01])[- /.](0?[1-9]|1[0-2])[- /.](19|20)\d\d\b')
matches = pattern.findall('The date is 08/09/2023')
for match in matches:
print(match)
let pattern = /\b(0?[1-9]|[12]\d|3[01])[- /.](0?[1-9]|1[0-2])[- /.](19|20)\d\d\b/g;
let result = 'The date is 08/09/2023'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
let pattern = /\b(0?[1-9]|[12]\d|3[01])[- /.](0?[1-9]|1[0-2])[- /.](19|20)\d\d\b/g;
let result = 'The date is 08/09/2023'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b(0?[1-9]|[12]\\d|3[01])[- /.](0?[1-9]|1[0-2])[- /.](19|20)\\d\\d\\b");
Matcher matcher = pattern.matcher("The date is 08/09/2023");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Matches phone numbers in various formats such as (123) 456-7890, 123-456-7890, or 123.456.7890.
import re
pattern = re.compile(r'\b\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}\b')
matches = pattern.findall('Call me at (123) 456-7890.')
for match in matches:
print(match)
let pattern = /\b\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}\b/g;
let result = 'Call me at (123) 456-7890.'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
let pattern = /\b\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}\b/g;
let result = 'Call me at (123) 456-7890.'.match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b\\(\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}\\b");
Matcher matcher = pattern.matcher("Call me at (123) 456-7890.");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
In this section, we delve into how you can use regex to extract values in Python and TypeScript.
Using the re
module in Python, you can use the findall
method to extract all matching strings from a given text.
import re
pattern = re.compile('regex_pattern_here')
matches = pattern.findall('your_text_here')
for match in matches:
print(match)
In TypeScript, you can use the match
method on a string to find all matches against a regex pattern.
let pattern = /regex_pattern_here/g;
let result = "your_text_here".match(pattern);
if (result) {
result.forEach(match => console.log(match));
}
- Always start your regex patterns with
r
in Python to avoid dealing with backslashes. - Utilize online tools like regex101 to test and debug your regex patterns.
- When possible, comment your regex to make it more readable for others (and for yourself in the future).
- Remember that some characters have special meanings in regex and need to be escaped with a backslash (
\
) if you want to match them literally.
-
Historical Background: The concept of regular expressions finds its roots in the 1950s, formulated by mathematician Stephen Kleene to characterize regular sets in a mathematical notation.
-
World Record Regex: The "regex.alf.nu" website hosted a regex contest where participants were tasked to match strings using the least bytes of regex code, showcasing the optimization capabilities of regex patterns.
-
Regex in Popular Culture: Regex patterns have occasionally graced the big screen, featured in movies and TV shows where characters use them to crack complex problems or hack into systems, highlighting their tech world prominence.
-
Google Code Jam: Google's Code Jam competition has featured numerous challenges solvable through the clever use of regex, a testament to its practical application in real-world coding scenarios.
This guide was collaboratively crafted with the support of OpenAI's ChatGPT, designed to be a quick and handy reference for developers.