📜  Linux正则表达式

📅  最后修改于: 2020-12-08 05:06:08             🧑  作者: Mango

Linux正则表达式

正则表达式也称为regex或regexp 。它是Linux中非常强大的工具。正则表达式是遵循某种模式的匹配字符串的模式。

正则表达式可用于各种程序,例如grep,sed,vi,bash,重命名等。

正则表达式元字符

正则表达式可以具有一个或几个重复的元字符。

Metacharacter Description
. Replaces any character.
^ Matches start of string and represents characters not in the string.
$ Matches end of string.
* Matches zero or more times the preceding character.
\ Represents the group of characters.
() Groups regular expressions.
? Matches exactly one character.
+ Matches one or more times the preceding character.
{N} Preceding character is matched exactly N times.
{N,} Preceding character is matched exactly N times or more.
{N,M} Preceding character is matched exactly N times, but not more than N times.
Represents the range.
\b Matches empty string at the edge of a word.
\B Matches empty string if it is not at the edge of a word.
\< Matches empty string at the beginning of a word.
\> Matches empty string at the end of a word.

正则表达式版本

正则表达式语法有三种版本:

  • BRE:基本正则表达式
  • ERE:扩展正则表达式
  • PRCE:Perl正则表达式

根据工具或程序,可以使用这些版本中的一个或多个。