正则表达式列表

术语

含义及用法

任何字符

任何给定的字符,除非该字符是正则表达式中的元字符。正则表达式的元字符清单位于该表的以下部分。

.

任何单个字符,除了换行符或者段落标记。例如,搜索“sh.rt”将会同时匹配“shirt”和“short”。

^

段落或者单元格的起始位置。位于段落开始位置的特殊对象(例如空的字段、锚定到字符的框架)将会被忽略。例如:“^Peter”会匹配单词“Peter”,仅当该单词为段落的第一个单词时。

$

段落或者单元格的结束位置。位于段落结束位置的特殊对象(例如空的字段、锚定到字符的框架)将会被忽略。例如:“Peter$”会匹配单词“Peter”,仅当该单词为段落的最后一个单词时,注意“Peter”后面如果还有一个英文句号那么匹配会失败。

单独的 $ 可匹配段落末尾。这样就可以查找替换段落标记了。

*

正则表达式字符之前紧邻的一个字符出现零次或多次。例如,“Ab*c”匹配“Ac”,“Abc”,“Abbc”,“Abbbc”等等。

+

正则表达式字符之前紧邻的一个字符出现一次或多次。例如,“AX.+4”匹配“AXx4”,但是无法匹配“AX4”。

这种模式下,会匹配段落中尽可能长的字符串。如果段落中包含字符串“AX 4 AX4”,那么整个这个字符串会被匹配到并被高亮。

?

正则表达式字符之前紧邻的一个字符出现零次或一次。例如,“Texts?”匹配“Text”和“Texts”,“x(ab|c)?y”会找到“xy”,“xaby”或者“xcy”。

\

转义字符。在该字符之后的单个特殊字符将会被解析为正常字符,而不是作为正则表达式元字符,但 "\n", "\t", "\b", "\>" and "\<" 例外。比如,“tree\.”会匹配“tree.”,而不能匹配“treed”或者“trees”。

\n

When entered in the Find text box, finds a line break that was inserted with the Shift+Enter key combination in Writer, or the Ctrl+Enter key combination in a Calc cell.

When entered in the Replace text box in Writer, inserts a paragraph break that can be inserted with the Enter or Return key. Has no special meaning in Calc, and is treated literally there.

To change line breaks into paragraph breaks in Writer, enter \n in both the Find and Replace boxes, and then perform a search and replace.

\t

A tab character. Can also be used in the Replace box.

\b

A word boundary. For example, "\bbook" matches "bookmark" and "book" but not "checkbook" whereas "book\b" matches "checkbook" and "book" but not "bookmark".

Note, this form replaces the obsolete (although they still work for now) forms "\>" (match end of word) and "\<" (match start of word).

^$

查找空段落。

^.

查找段落的首字符。

& 或 $0

Adds the string that was found by the search criteria in the Find box to the term in the Replace box when you make a replacement.

For example, if you enter "window" in the Find box and "&frame" in the Replace box, the word "window" is replaced with "windowframe".

You can also enter an "&" in the Replace box to modify the Attributes or the Format of the string found by the search criteria.

[...]

Any single occurrence of any one of the characters that are between the brackets. For example: "[abc123]" matches the characters ‘a’, ‘b’, ’c’, ‘1’, ‘2’ and ‘3’. "[a-e]" matches single occurrences of the characters a through e, inclusive (the range must be specified with the character having the smallest Unicode code number first). "[a-eh-x]" matches any single occurrence of the characters that are in the ranges ‘a’ through ‘e’ and ‘h’ through ‘x’.

[^...]

Any single occurrence of a character, including Tab, Space and Line Break characters, that is not in the list of characters specified inclusive ranges are permitted. For example "[^a-syz]" matches all characters not in the inclusive range ‘a’ through ‘s’ or the characters ‘y’ and ‘z’.

\uXXXX

\UXXXXXXXX

The character represented by the four-digit hexadecimal Unicode code (XXXX).

The character represented by the eight-digit hexadecimal Unicode code (XXXXXXXX).

For certain symbol fonts the symbol (glyph) that you see on screen may look related to a different Unicode code than what is actually used for it in the font. The Unicode codes can be viewed by choosing Insert - Special Character, or by using Unicode conversion shortcut.

|

The infix operator delimiting alternatives. Matches the term preceding the "|" or the term following the "|". For example, "this|that" matches occurrences of both "this" and "that".

{N}

The post-fix repetition operator that specifies an exact number of occurrences ("N") of the regular expression term immediately preceding it must be present for a match to occur. For example, "tre{2}" matches "tree".

{N,M}

The post-fix repetition operator that specifies a range (minimum of "N" to a maximum of "M") of occurrences of the regular expression term immediately preceding it that can be present for a match to occur. For example, "tre{1,2}" matches "tre" and "tree".

{N,}

The post-fix repetition operator that specifies a range (minimum "N" to an unspecified maximum) of occurrences of the regular expression term immediately preceding it that can be present for a match to occur. (The maximum number of occurrences is limited only by the size of the document). For example, "tre{2,}" matches "tree", "treee", and "treeeee".

(...)

The grouping construct that serves three purposes.

  1. To enclose a set of ‘|’ alternatives. For example, the regular expression "b(oo|ac)k" matches both "book" and "back".

  2. To group terms in a complex expression to be operated on by the post-fix operators: "*", "+" and "?" along with the post-fix repetition operators. For example, the regular expression "a(bc)?d" matches both "ad" and "abcd" in a search.; the regular expression "M(iss){2}ippi" matches "Mississippi".

  3. To record the matched sub string inside the parentheses as a reference for later use in the Find box using the "\n" construct or in the Replace box using the "$n" construct. The reference to the first match is represented by "\1" in the Find box and by "$1" in the Replace box. The reference to the second matched sub string by "\2" and "$2" respectively, and so on.

For example, the regular expression "(890)7\1\1" matches "8907890890".

With the regular expression "\b(fruit|truth)\b" in the Find box and the regular expression "$1ful" in the Replace box occurrences of the words "fruit" and "truth" can be replaced with the words "fruitful" and "truthful" respectively without affecting the words "fruitfully" and "truthfully"

[:alpha:]

表示字母字符。使用「[:alpha:]+」来查找它们中的任意一个。

[:digit:]

表示十进制数字。使用「[:digit:]+」来查找它们中的任意一个。

[:alnum:]

代表一个字母数字字符 (字母和数字)。

[:space:]

代表空格字符 (但不代表其他空白字符)。

[:print:]

代表一个可打印字符。

[:cntrl:]

代表一个非打印字符。

[:lower:]

如果在「选项」中选择「区分大小写」,则显示小写字符。

[:upper:]

如果在「选项」中选中「区分大小写」,则代表一个大写字符。


For a full list of supported metacharacters and syntax, see ICU Regular Expressions documentation

Regular expression terms can be combined to form complex and sophisticated regular expressions for searches as show in the following examples.

示例

Expression

Meaning

^$

An empty paragraph.

^ specifies that the match must be at the start of a paragraph,

$ specifies that a paragraph mark or the end of a cell must follow the matched string.

^.

The first character of a paragraph.

^ specifies that the match must be at the start of a paragraph,

. specifies any single character.

e([:digit:])?

Matches "e" by itself or an "e" followed by one digit.

e specifies the character "e",

[:digit:] specifies any decimal digit,

? specifies zero or one occurrences of [:digit:].

^([:digit:])$

Matches a paragraph or cells containing exactly one digit.

^ specifies that the match must be at the start of a paragraph,

[:digit:] specifies any decimal digit,

$ specifies that a paragraph mark or the end of a cell must follow the matched string.

^[:digit:]{3}$

Matches a paragraph or cell containing only three digit numbers

^ specifies that the match must be at the start of a paragraph,

[:digit:] specifies any decimal digit,

{3} specifies that [:digit:] must occur three times,

$ specifies that a paragraph mark or the end of a cell must follow the matched string.

\bconst(itu|ruc)tion\b

Matches the words "constitution" and "construction" but not the word "constitutional."

\b specifies that the match must begin at a word boundary,

const specifies the characters "const",

( starts the group,

itu specifies the characters "itu",

| specifies the alternative,

ruc specifies the characters "ruc",

) ends the group,

tion specifies the characters "tion",

\b specifies that the match must end at a word boundary.