Regular Expression Tools

Regex Whitespace

Python

In Python, the \s character in regular expressions is used to match any whitespace character:

import re

pattern = re.compile(r'\s')
match = pattern.match(' ')

To match one or more whitespace characters, you can use the + quantifier:

pattern = re.compile(r'\s+')
match = pattern.match('   ')

C#

In C#, \s is used to match any whitespace character:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"\s");
        Match match = regex.Match(" ");
    }
}

To match one or more whitespace characters, use the + quantifier:

Regex regex = new Regex(@"\s+");
Match match = regex.Match("   ");

PHP

In PHP, \s is used to match any whitespace character:

$match = preg_match('/\s/', ' ');

To match one or more whitespace characters, use the + quantifier:

$match = preg_match('/\s+/', '   ');

SQL

In SQL, regular expression support may vary, but generally, to match whitespace, you can use the [[:space:]] character class in SQL's REGEXP or similar functions:

SELECT * FROM table WHERE column REGEXP '[[:space:]]';

This will match any string in the specified column containing at least one whitespace character.

JavaScript

In JavaScript, \s is used to match any whitespace character:

let regex = new RegExp('\\s');
let match = regex.test(' ');

To match one or more whitespace characters, use the + quantifier:

let regex = new RegExp('\\s+');
let match = regex.test('   ');

Java

In Java, \s is used to match any whitespace character:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\s");
        Matcher matcher = pattern.matcher(" ");
        boolean match = matcher.find();
    }
}

To match one or more whitespace characters, use the + quantifier:

Pattern pattern = Pattern.compile("\\s+");
Matcher matcher = pattern.matcher("   ");
boolean match = matcher.find();

Conclusion

Matching any whitespace character across different programming languages generally involves the use of the \s character in regular expressions. This character enables developers to identify and manipulate whitespace characters in strings efficiently, ensuring better data handling and processing across various programming environments.

Copyright © 2019 - RegExpLib.com