代码改变世界

UltraEdit正则表达式

2011-09-14 19:49  clarkhan  阅读(544)  评论(0编辑  收藏  举报

UltraEdit中可以设置正则表达式引擎,缺省的是UltraEdit样式。可以在“配置 - 搜索”中修改选项:
Perl兼容正则表达式:如果选中该选项,则编辑器将基于 Boost C++ 库使用Perl样式的正则表达式(当在查找/替换对话框里选中正则表达式时)。
UltraEdit样式正则表达式:如果选中该选项,则编辑器将在查找/替换对话框里选中正则表达式时使用UltraEdit样式的正则表达式。
UNIX样式正则表达式:如果选中该选项,则编辑器将在查找/替换对话框里选中正则表达式时使用UNIX样式的正则表达式。
选择自己喜好的正则表达式类型,不同的引擎语法不同,比如Perl兼容的正则表达式,回车是\r,而在UltraEdit风格的以及Unix兼容的回车则分别是^p和\n。

UltraEdit 允许在搜索菜单下面列出的大多数搜索和替换功能中使用正则表达式。

正则表达式允许在一次操作中执行多个组合搜索和替换功能。

这里有两个可以使用的语法集。下面的第一个表显示用于早期 UltraEdit 的原始 UltraEdit 语法。第二个表显示可选的“Unix”样式的正则表达式。这可以从配置段启用。

正则表达式 (UltraEdit 语法):

符号

功能

%

匹配行首 - 表示搜索字符串必须在行首,但不包括任何选定的结果字符中的行终止字符。

$

匹配行尾 - 表示搜索字符串必须在行尾,但不包括任何选定的结果字符中的行终止字符。

?

匹配任何除换行符的字符。

*

匹配任何除换行符外所出现的任意数量的字符。

+

匹配一个或多个前面的字符/表达式。必须找到至少一个出现的字符。不匹配重复的换行符。

++

0 次或多次匹配前面的字符/表达式。不匹配重复的换行符。

^b

匹配一个分页符。

^p

匹配一个换行符 (CR/LF) (段落) (DOS 文件)

^r

匹配一个换行符 (仅 CR) (段落) (MAC 文件)

^n

匹配一个换行符 (仅 LF) (段落) (UNIX 文件)

^t

匹配一个制表符

[ ]

匹配任何括号中的单个字符或范围

^{A^}^{B^}

匹配表达式 A 或 B

^

忽略其后的正则表达式字符

^(*^)

在表达式加上括号或标签在替换命令中使用。正则表达式中可以有 9 个表达式标签,数字根据它们在正则表达式中的次序确定数字。

相应的替换表达式是 ^x,x 的范围是 1-9。例如: 如果 ^(h*o^) ^(f*s^) 匹配“hello folks”,那么^2 ^1 表示将用“folks hello”替换它。

注意 - ^ 这里涉及的字符“^”不是控制键 + 值。

例如:

m?n 匹配“man”、“men”、“min”,但不匹配“moon”。

t*t 匹配“test”、“tonight”和“tea time”中的“tea t”部分,但不匹配“tea

time” (“tea ”和“time”之间有换行)。

Te+st 匹配“test”、“teest”、“teeeest”等,但不匹配“tst”。

[aeiou] 匹配每个元音小写字母

[,.?] 匹配文字“,”、“.”或“?”。

[0-9a-z] 匹配任何数字或小写字母

[~0-9] 匹配除数字外的任何字符 (~ 表示不匹配其后的内容)

你可以搜索象下面一样的表达式 A 或 B:

"^{John^}^{Tom^}

这将搜索 John 或 Tom。在两个表达式之间应该没有任何其它内容。

你可以在同一次搜索象下面一样组合 A 或 B 和 C 或 D:

"^{John^}^{Tom^} ^{Smith^}^{Jones^}"

这将搜索后面跟随了 Smith 或 Jones 的 John 或 Tom。

下面的表显示“Unix”样式的正则表达式语法。

正则表达式 (Unix 语法):

符号

功能

\

表示下一个字符有特殊含义。“n”表示匹配字符“n”,“\n”匹配一换行符。看下面的例子 (\d、\f、\n 等)。

^

匹配/停驻行首。

$

匹配/停驻行尾。

*

匹配前面的字符 0 次或多次。

+

匹配前面的字符一次或多次。不匹配重复的换行符。

.

匹配任何除换行符之外的单个字符。不匹配重复的换行符。

(表达式)

在表达式加上括号或标签在替换命令中使用。正则表达式中可以有 9 个表达式标签,数字根据它们在正则表达式中的次序确定数字。

相应的替换表达式是 ^x,x 的范围是 1-9。例如: 如果 ^(h*o^) ^(f*s^) 匹配“hello folks”,那么^2 ^1 表示将用“folks hello”替换它。

[xyz]

字符集,匹配任何括号间的字符。.

[^xyz]

排除字符集。匹配任何不在括号间的字符。

\d

匹配一个数字字符。等同于 [0-9]。

\D

匹配一个非数字字符,等同于 [^0-9]。

\f

匹配一个换页符。

\n

匹配一个换行符。

\r

匹配一个回车符。

\s

匹配任何包含空格、制表符等不会显示的字符,但不匹配换行符。

\S

匹配任何非空白区域 (显示字符) 的字符,但不匹配换行符。

\t

匹配一个制表符。

\v

匹配一个垂直制表符。

\w

匹配任何包含下划线的词语。

\W

匹配任何非词语的字符。

\p

匹配 CR/LF (等同于 \r\n),用来匹配 DOS 行终止符。

注意 - ^ 这里所涉及的字符“^”不是控制键 + 值。

例如:

m.n 匹配“man”、“men”、“min”,但不匹配“moon”。

Te+st 匹配“test”、“teest”、“teeeest”等,但不匹配“tst”。

Te*st 匹配“test”、“teest”、“teeeest”等,还有“tst”。

[aeiou] 匹配每个元音小写字母

[,.?] 匹配文字“,”、“.”或“?”。

[0-9a-z] 匹配任何数字或小写字母

[^0-9] 匹配除数字外的任何字符 (~ 表示不匹配其后的内容)

你可以象下面一样的表达式 A 或 B 进行搜索:

"(John|Tom)"

这将搜索 John 或 Tom。在两个表达式之间应该没有任何其它内容。

你可以在同一次搜索象下面一样组合 A 或 B 和 C 或 D:

"(John|Tom) (Smith|Jones)"

这将搜索 Smith 或 Jones 以及跟随在后面的 John 或 Tom。

如果在查找/替换中没有选择使用正则表达式,在替换对象中下列特殊字符同样有效:

符号

功能

^^

匹配字符“^”

^s

表示活动文件窗口中选定 (加亮) 的文字。

^c

表示剪贴表的内容。

^b

匹配一个分页符。

^p

匹配一个换行符 (CR/LF) (段落) (DOS 文件)

^r

匹配一个换行符 (仅 CR) (段落) (MAC 文件)

^n

匹配一个换行符 (仅 LF) (段落) (UNIX 文件)

^t

匹配一个制表符

注意 - ^ 这里所涉及的字符“^”不是控制键 + 值。

UltraEdit/UEStudio now support Perl style regular expressions using the Boost C++ Libraries. The Perl regular expression syntax is based on that used by the programming language Perl.

Perl Regular Expression Syntax

In Perl regular expressions, all characters match themselves except for the following special characters:

.[{()\*+?|^$

Wildcard

The single character '.' when used outside of a character set will match any single character.

Anchors

A '^' character shall match the start of a line.

A '$' character shall match the end of a line.

Marked sub-expressions

A section beginning ( and ending ) acts as a marked sub-expression.  Whatever matched the sub-expression is split out in a separate field by the matching algorithms.  Marked sub-expressions can also repeated, or referred to by a back-reference.

Alternation

The | operator will match either of its arguments, so for example: abc|def will match either "abc" or "def".

Parenthesis can be used to group alternations, for example: ab(d|ef) will match either of "abd" or "abef".

Empty alternatives are not allowed (these are almost always a mistake), but if you really want an empty alternative use (?:) as a placeholder, for example:

"|abc" is not a valid expression, but

"(?:)|abc" is and is equivalent, also the expression:

"(?:abc)??" has exactly the same effect.

Character sets

A character set is a bracket-expression starting with [ and ending with ], it defines a set of characters, and matches any single character that is a member of that set.

A bracket expression may contain any combination of the following:

Single characters

For example [abc], will match any of the characters 'a', 'b', or 'c'.

Character ranges

For example [a-c] will match any single character in the range 'a' to 'c'.  By default, for POSIX-Perl regular expressions, a character x is within the range y to z, if it collates within that range; this results in locale specific behavior.

Negation

If the bracket-expression begins with the ^ character, then it matches the complement of the characters it contains, for example [^a-c] matches any character that is not in the range a-c.

Character classes

An expression of the form [[:name:]] matches the named character class "name", for example [[:lower:]] matches any lower case character.  The following character class names are always supported:

Name

POSIX-standard

description1

alnum

Yes

Any alpha-numeric character.

alpha

Yes

Any alphabetic character.

blank

Yes

Any whitespace character that is not a line separator.

cntrl

Yes

Any control character.

d

No

Any decimal digit

digit

Yes

Any decimal digit.

graph

Yes

Any graphical character.

l

No

Any lower case character.

lower

Yes

Any lower case character.

print

Yes

Any printable character.

punct

Yes

Any punctuation character.

s

No

Any whitespace character.

space

Yes

Any whitespace character.

unicode

No

Any extended character whose code point is above 255 in value.

u

No

Any upper case character.

upper

Yes

Any upper case character.

w

No

Any word character (alphanumeric characters plus the underscore).

word

No

Any word character (alphanumeric characters plus the underscore).

xdigit

Yes

Any hexadecimal digit character.

Escapes

Any special character preceded by an escape shall match itself.   The following escape sequences are also supported:

Escapes matching a specific character

The following escape sequences are all synonyms for single characters:

Escape

Character

\a

'\a'

\e

0x1B

\f

\f

\n

\n

\r

\r

\t

\t

\v

\v

\b

\b (but only inside a character class declaration).

\cX

An ASCII escape sequence - the character whose code point is X % 32

\xdd

A hexadecimal escape sequence - matches the single character whose code point is 0xdd.

\x{dddd}

A hexadecimal escape sequence - matches the single character whose code point is 0xdddd.

\0ddd

An octal escape sequence - matches the single character whose code point is 0ddd.

\N{name}

Matches the single character which has the symbolic name name.  For example \N{newline} matches the single character \n.

"Single character" character classes

Any escaped character x, if x is the name of a character class shall match any character that is a member of that class, and any escaped character X, if x is the name of a character class, shall match any character not in that class.  The following are supported by default:

Escape sequence

Equivalent to

\d

[[:digit:]]

\l

[[:lower:]]

\s

[[:space:]]

\u

[[:upper:]]

\w

[[:word:]]

\D

[^[:digit:]]

\L

[^[:lower:]]

\S

[^[:space:]]

\U

[^[:upper:]]

\W

[^[:word:]]

Word Boundaries

The following escape sequences match the boundaries of words:

\<

Matches the start of a word.

\>

Matches the end of a word.

\b

Matches a word boundary (the start or end of a word).

\B

Matches only when not at a word boundary.

Symbol Function
^^ searches for a "^" character
^s searches for selected (highlighted) text when a macro is running
^c searches for the contents of the clipboard when a macro is running
^b matches a page break
^p matches a newline (CR/LF) (paragraph) (DOS Files)
^r matches a newline (CR Only) (paragraph) (MAC Files)
^n matches a newline (LF Only) (paragraph) (UNIX Files)
^t matches a tab character

For further information/options please see the Boost Libraries Perl Regular Expression syntax pages.

notepad++

\\ - Backslash character;
\t – TAB character;
\r – CR character;
\n – LF character;
\0 – NULL character;
\x## - Hexadecimal value (between 00 and FF);
\u#### - Extended hexadecimal value (between 0000 and FFFF, meant for Unicode characters);
\d### - Decimal value (between 000 and 255);
\o### - Octal value (between 000 and 377);
\b######## - Binary value (between 00000000 and 11111111).