Now that we’ve seen the basic syntax (more or less), it’s a common syntax that we can apply in different programming languages and tools.
However, although the basic syntax of regular expressions is usually similar, each language can have variations in its implementation and in the available functions.
So, let’s dedicate an article to see how we could execute some regular expressions in Python, JavaScript, and C#.
Regular Expressions in Python
Python offers the re module, which provides a wide range of functionalities for working with regular expressions. Below are some practical examples.
Basic Matches
import re
text = "The cat is on the roof."
pattern = r'cat'
# Find matches
matches = re.findall(pattern, text)
print(matches) # ['cat']
Substitution
text = "The sky is blue."
pattern = r'blue'
replacement = 'red'
# Replace matches
new_text = re.sub(pattern, replacement, text)
print(new_text) # 'The sky is red.'
Using Flags
text = "Hello, Hello, hELLO"
pattern = r'hello'
# Search case-insensitively
matches = re.findall(pattern, text, re.IGNORECASE)
print(matches) # ['Hello', 'Hello', 'hELLO']
Regular Expressions in JavaScript
JavaScript has native support for regular expressions through the RegExp class. Below are examples of its use.
Basic Matches
let text = "The cat is on the roof.";
let pattern = /cat/;
let matches = text.match(pattern);
console.log(matches); // ['cat']
Substitution
let text = "The sky is blue.";
let pattern = /blue/;
let replacement = 'red';
let newText = text.replace(pattern, replacement);
console.log(newText); // 'The sky is red.'
Using Flags
let text = "Hello, Hello, hELLO";
let pattern = /hello/gi; // 'g' for global, 'i' for case-insensitive
let matches = text.match(pattern);
console.log(matches); // ['Hello', 'Hello', 'hELLO']
Regular Expressions in Java
Java provides the java.util.regex package for working with regular expressions. Below are examples.
Basic Matches
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String text = "The cat is on the roof.";
String pattern = "cat";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group()); // 'cat'
}
}
}
Substitution
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String text = "The sky is blue.";
String pattern = "blue";
String replacement = "red";
String newText = text.replaceAll(pattern, replacement);
System.out.println(newText); // 'The sky is red.'
}
}
Using Flags
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String text = "Hello, Hello, hELLO";
String pattern = "(?i)hello"; // 'i' for case-insensitive
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group()); // 'Hello', 'Hello', 'hELLO'
}
}
}
Regular Expressions in C#
In C#, regular expressions are handled through the Regex class in the System.Text.RegularExpressions namespace. Here are usage examples.
Basic Matches
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = "The cat is on the roof.";
string pattern = "cat";
Match match = Regex.Match(text, pattern);
if (match.Success)
{
Console.WriteLine(match.Value); // 'cat'
}
}
}
Substitution
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = "The sky is blue.";
string pattern = "blue";
string replacement = "red";
string newText = Regex.Replace(text, pattern, replacement);
Console.WriteLine(newText); // 'The sky is red.'
}
}
Using Flags
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = "Hello, Hello, hELLO";
string pattern = "hello";
MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
Console.WriteLine(match.Value); // 'Hello', 'Hello', 'hELLO'
}
}
}
