4. Posix基本正则表达式

Posix基本正则表达式可实现不同的工具之间保持一致性,例如grep和awk。对于传统的unix工具,并没有实现扩展正则表达式。

历史

传统的unix正则表达式语法在各个工具之间都不同,posix正则表达式被ieee开发,它还具有一套宽展语法。这些标准的设计考虑到尽可能兼容简单正则表达式语法,并为unix工具提供一套标准的语法。

语法

在posix基本正则表达式语法中,大多数字符都被理解为字面意思,它们只匹配它们自己。除此之外为元字符,元字符的定义如下表。

元字符 描述
. 匹配任何单一字符(许多应用程序不对换行符进行匹配,因为换行符根据字符编码和平台相关,但是假设总是匹配换行符是安全的),在posix括号表达式“[]”中,“.”字符总是匹配本身,例如[a.c]匹配“a”、“.”或“c”。
[ ] 匹配括号中的单一字符,例如[abc]匹配“a”、“b”或“c”,[a-z]表示匹配小写字母a到小写字母 z的一个范围。这种形式可以混合使用,如[abcx-z]表示匹配”a”、“b”、“c”、“x”、“y”和”z”,[a-cx-z]具有同样效果。如果“-”是括号中的第一个或者最后一个字符,那么它将理解为字面含义“-”。“]”字符可以出现在括号表达式中,如果他是第一个字符的话,例如“[]abc]”。括号表达式中也可包含”字符类”,“等价类”和”整理字符”。
[^ ] 匹配除了括号中的任一字符,结构和用法同“[]”。
^ 如果是正则表达式的第一个字符,将匹配字符串的起始位置。
$ 如果是正则表达式的最后一个字符,将在字符串的结尾处进行匹配
* 匹配前一个匹配元素0次或多次,如“ab*c”将匹配”ac”,“abc”和“abbbc”等。“[xyz]*”匹配“”,“x”,”y”,“z”,“zx”,“zyx”,“xyzzy”等。
BRE: \{m\}
ERE: {m}
指定前一个匹配元素的匹配次数,如”a\{3\}”仅匹配“aaa”。
BRE: \{m,\}
ERE: {m,}
匹配前一个匹配元素最少m次,例如“a\{3,\}”匹配“aaa”,“aaaa”,”aaaaa”等。
BRE: \{m,n\}
ERE: {m,n}
匹配前一个匹配元素最少m次,最多n次,例如“a\{3,5\}”匹配“aaa”,”aaaa”,“aaaaa”。
BRE: \( \)
ERE: ( )
定义一个子表达式,将对待该元素为单一元素,例如“ab*”匹配“a”,“ab”,“abb”等。而“\(ab\)*”匹配”“,”abab”,”ababab“等。被匹配的字符串稍后可以被调用,请参考”\n“。
BRE only: \n 取得第n个子表达式的值,n为1到9。This construct is theoretically irregular (an expression with this construct does not obey the mathematical definition of regular expression), and was not adopted in the POSIX ERE syntax.

示例:

  • .at 匹配任何以”at“结尾的三字符的字符串
  • [hc]at 匹配hat和cat
  • [^b]at 匹配任何以”at“结尾的三字符的字符串,除了bat
  • ^[hc]at 匹配字符串或行开始处的hat或cat
  • [hc]at$ 在字符串的结尾处匹配hat或cat
  • \[.\] 匹配任一被[和]包含的字符,如”[a]”和”[b]”.

字符类

Posix标准定义了一系列字符类,这些字符类在方括号中使用。

POSIX class 相似于 意义
[:upper:] [A-Z] 大写字符
[:lower:] [a-z] 小写字符
[:alpha:] [A-Za-z] 大写字符和小写字符
[:digit:] [0-9] 数字
[:xdigit:] [0-9A-Fa-f] 十六进制数
[:alnum:] [A-Za-z0-9] 数字和大小写字符
[:punct:] 除了字母和数字的所有可见(graphic)字符
[:blank:] [ \t] 空格和tab
[:space:] [ \t\n\r\f\v] 空白字符
[:cntrl:] 控制字符
[:graph:] [^ [:cntrl:]] 可见字符 (all characters which have graphic representation)
[:print:] [[:graph] ] 可见字符和空格

示例:

  • a[[:digit:]]b 匹配”a0b”, “a1b”, …, “a9b”。
  • a[:digit:]b is an error: character classes must be in brackets
  • [[:digit:]abc] matches any digit, “a”, “b”, and “c”.
  • [abc[:digit:]] is the same as above
  • [^ABZ[:lower:]] matches any character except lowercase letters, A, B, and Z.

整理字符

Collating symbols, like character classes, are used in brackets and have the form [.ch.]. Here ch is a digraph. Collating systems are defined by the locale.

Equivalence classes

Equivalence classes, like character classes and collating symbols, are used in brackets and have the form [=a=]. They stand for any character which is equivalent to the given. According to the standard[1], For example, if ‘a’, ‘à’, and ‘â’ belong to the same equivalence class, then “[[=a=]b]”, “[[=à=]b]”, and “[[=â=]b]” are each equivalent to “[aàâb]”. Equivalence classes, like collating symbols, are defined by the locale.

External links

IEEE Std 1003.1, 2004 Edition. Chapter 9, Regular Expressions.

Use in Tools

Tools and languages that utilize this regular expression syntax include:

Links

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注