When dealing with the text files such as log files, user list, server list etc we can use regex for formally structured files. Space, white-space, and tab are popular separating elements used in regex or CSV files. In this tutorial, we will examine how to use regex with space, whitespace, tab or no space, no whitespace and no tab.

当处理诸如日志文件,用户列表,服务器列表等文本文件时,我们可以将正则表达式用于形式化的文件。 空格,空格和制表符是正则表达式或CSV文件中常用的分隔元素。 在本教程中,我们将研究如何在带空格,空格,制表符或无空格,无空格和无制表符的情况下使用正则表达式。

示例文字 (Example Text)

We will use the following text as an example.

我们将使用以下文本作为示例。

This is a test text.
We     will provide different lines.
For example this sentence contains spaces and tabs.
Thislinedonotcontainsanyspaceortab.

正则表达式空格或空白 (Regex Space or Whitespace)

The regular expression is expressed as \s in the regex language. We can use single or multiple \s without a problem. We will use egrep command which is used to run regular expressions on given text or file. In this example, we will search for spaces in the file named example.txt

正则表达式用正则表达式语言表示为\s 。 我们可以毫无问题地使用单个或多个\s 。 我们将使用egrep命令,该命令用于在给定的文本或文件上运行正则表达式。 在此示例中,我们将在名为example.txt的文件中搜索空格

$ egrep "\s" example.txt
Regex Space or Whitespace
正则表达式空格或空白

正则表达式忽略空格或空白(Regex Ignore Space or Whitespace)

If we want to skip the space or whitespace in the given text we will use -v before the \S. In this example, we will only print the lines that do not contain any space.

如果要跳过给定文本中的空格或空白,我们将在\S之前使用-v 。 在此示例中,我们将仅打印不包含任何空格的行。

$ egrep -v "\S" example.txt
Regex Ignore Space or Whitespace
正则表达式忽略空格或空白

正则表达式选项卡(Regex Tab)

The tab is a whitespace character which contains multiple spaces. We can maths those contains tabs with the \t like below.

该选项卡是包含多个空格的空白字符。 我们可以使用\t对包含这些选项卡的数学运算,如下所示。

"\t"

PHP中的正则表达式空间 (Regex Space In PHP)

PHP provides all features of the regular expressions. We can math lines that contain spaces with the preg_match() function like below. We will put matches to the $mathes variable and print with the print_r() function.

PHP提供了正则表达式的所有功能。 我们可以使用preg_match()函数对包含空格的行进行数学运算,如下所示。 我们将匹配项放入$mathes变量,并使用print_r()函数进行打印。

<?php

$text="This is a space delimited line.";

$pattern="\s";

preg_match($pattern,$text,$mathes);

print_r($mathes);

?>

Python中的正则表达式空间 (Regex Space In Python)

Python language provides the match() function from re module. We can use \s with python in order to match spaces like below.

Python语言从re模块提供match()函数。 我们可以将\s与python配合使用以匹配如下所示的空格。

#!/bin/python

import re

text="This is a space delimited line."

re.match(r'\s',text)

JavaScript中的正则表达式空间 (Regex Space In JavaScript)

Javascript also provides regex functionality to match spaces in the text. We can use /\s/g in order to match spaces with the regular expression.

Javascript还提供了正则表达式功能来匹配文本中的空格。 我们可以使用/\s/g来将空格与正则表达式匹配。

var str="This is a space delimited line";

var mathes=/\s/g.exec(str);
LEARN MORE  Linux Bash Shell Printf Command Examples 了解更多Linux Bash Shell Printf命令示例

翻译自: https://www.poftut/regex-space-whitespace-tab-usage-examples/

更多推荐

正则表达式,空格,制表符用法示例