Language: EN

regex-en-diferentes-lenguajes

Examples of Regular Expressions in Different Programming Languages

Now that we have seen the basic syntax (which is more or less) 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 may have variations in its implementation and the available functions.

So let’s dedicate an article to see how some regular expressions could be executed 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']

Replacement

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 usage.

Basic Matches

let text = "The cat is on the roof.";
let pattern = /cat/;

let matches = text.match(pattern);
console.log(matches);  // ['cat']

Replacement

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 to work 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'
        }
    }
}

Replacement

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'
        }
    }
}

Replacement

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'
        }
    }
}