正则表达式作为模式匹配,经常用于查找/替换操作的特定字符串。使用正则表达式来简化操作和提高效率的方式有许多。下面列出了一个用于ultra - edit样式和unix样式正则表达式的参考以及一些示例,演示如何在ultra - edit中使用正则表达式
Regular Expressions in UltraEdit | ||
UltraEdit Symbol | UNIX Symbol | Function |
% | ^ | Matches/anchors the beginning of line. 行首 |
$ | $ | Matches/anchors the end of line. 行尾 |
? | . | Matches any single character except a newline character. Does not match repeated newlines. 单个字符 |
* |
| Matches any number of occurrences of any character except newline. 0-n个字符 |
+ | + | Matches one or more of the preceding character/expression. At least one occurrence of the character must be found. Does not match repeated newlines. 1-n个字符 |
++ | * | Matches the preceding character/expression zero or more times. Does not match repeated newlines. |
^ | \ | Indicates the next character has a special meaning. "n" on its own matches the character "n". "^n" (UE expressions) or "\n" (UNIX expressions) matches a linefeed or newline character. 转义符. |
[ ] | [ ] | Matches any single character or range in the brackets. 括弧内匹配一个或某个范围的单字符 |
[~xyz] | [^xyz] | A negative character set. Matches any characters NOT between brackets.括弧内匹配除这个或这个范围的单字符 |
^b | \f | Matches a page break/form feed character.分页符 |
^p | \p | Matches a newline (CR/LF) (paragraph) (DOS Files). DOS换行符 |
^r | \r | Matches a newline (CR Only) (paragraph) (MAC Files).MAC换行符 |
^n | \n | Matches a newline (LF Only) (paragraph) (UNIX Files).UNIX换行符 |
^t | \t | Matches a tab character.制表符 |
[0-9] | \d | Matches a digit character.一个数字 |
[~0-9] | \D | Matches a non-digit character.除了数字的符号 |
[ ^t^b] | \s | Matches any white space including space, tab, form feed, etc., but not newline. 空格 |
[~ ^t^b] | \S | Matches any non-white space character but not newline. 非空格(除换行) |
| \v | Matches a vertical tab character.垂直制表符 |
[0-9a-z_] | \w | Matches any alphanumeric character including underscore. 一个标识符 |
[~0-9a-z_] | \W | Matches any character except alphanumeric characters and underscore. 非标识符 |
^{A^}^{B^} | (A|B) | Matches expression A OR B. 分支(A或B) |
^ | \ | Overrides the following regular expression character. |
^(...^) | (...) | Brackets or tags an expression to use in the replace command. A regular expression may have up to 9 tagged expressions, numbered according to their order in the regular expression. 子组 |
^1 | \1 | Numerical reference to tagged expressions. Text matched with tagged expressions may be used in Replace commands with this format. 反向引用子组的匹配值 |
Note: ^ refers to the character '^' NOT Control Key + value. ^是一个字符,不是Ctrl键 |
使用样例(UltraEdit/UNIX Regular Expression Examples)
简单字符串匹配(Simple String Matching)
简单的字符串匹配是正则表达式最基本的形式,不但快速而且可以方便地一次搜索多个字符串,无须执行多个查找操作。
UltraEdit RegExp:
Find What: m?n
Matches: "man" and "men" but not "moon"
Find What: t*t
Matches: "test", "tonight" and "tea time" (the "tea t" portion) but not "tea
time" (newline between "tea " and "time").
Find What: Te+st
Matches: "test", "teest", "teeeest", etc. but does not match "tst"
UNIX RegExp:
Find What: m.n
Matches: "man" and "men" but not "moon"
Find What: t.*t
Matches: "test", "tonight" and "tea time" (the "tea t" portion) but not "tea
time" (newline between "tea " and "time").
Find What: Te+st
Matches: "test", "teest", "teeeest", etc. but does not match "tst"
Character Sets
A character set is a group of characters bounded by "[" and "]". These may be used to designate specific characters to be matched or ranges (i.e. [aeud], or [a-z]).
UltraEdit RegExp:
Find What: [aeiou]
Matches: every vowel
NOTE: Regular Expressions in UltraEdit are not case-sensitive unless Match Case is selected in the Find dialog.
Find What: [,.^?]
Matches: a literal ",", "." or "?".
Because the "?" is a symbol used in expressions it must be "escaped" for the literal character to be matched rather than interpreted as an expression.
Find What: [0-9a-z]
Matches: any digit or letter
Find What: [~0-9]
Matches: any character except a digit (~ means NOT the following)
UNIX RegExp:
Find What: [aeiou]
Matches: every vowel
Find What: [,\.?]
Matches: a literal ",", "." or "?".
Because the "." is a symbol used in expressions it must be "escaped" for the literal character to be matched rather than interpreted as an expression.
Find What: [0-9a-z]
Matches: any digit or letter
Find What: [^0-9]
Matches: any character except a digit (^ means NOT the following)
OR Expressions
Currently UltraEdit only allows for the specification of two operands for an OR expression. You may search for an expression A or B as follows:
UltraEdit RegExp:
Find What: ^{John^}^{Tom^}
UNIX RegExp:
Find What: (John|Tom)
There should be nothing between the two expressions. You may combine A or B and C or D in the same search as follows:
UltraEdit RegExp:
Find What: ^{John^}^{Tom^} ^{Smith^}^{Jones^}
UNIX RegExp:
Find What: (John|Tom) (Smith|Jone)
This will search for "John" or "Tom" followed by "Smith" or "Jones".
Deleting Blank Lines
With Regular Expressions selected in the Replace dialog this will match the a CR/LF (DOS line terminator) immediately followed by the end of a line (i.e., a blank line) and replace it with nothing, effectively deleting it:
UltraEdit RegExp:
Find What: ^p$
Replace With: (literally nothing)
UNIX RegExp:
Find What: \p$
Replace With: (literally nothing)
Reformatting Text With Tagged Expressions
Example 1:
Tagged expressions may be used to mark various data members so that they may be reorganized, reformatting the data. For example, it might be useful to be able to rearrange:
John Smith, 385 Central Ave., Cincinnati, OH, 45238
into:
45238, Smith, John, 385 Central Ave., Cincinnati, OH
UltraEdit RegExp:
Find What: %^([a-z]+^) ^([a-z]+^), ^(*^), ^(*^), ^(*^), ^([0-9]+^)
Replace With: ^6, ^2, ^1, ^3, ^4, ^5
UNIX RegExp:
Find What: ^([a-z]+) ([a-z]+), (.*), (.*), (.*), ([0-9]+)
Replace With: \6, \2, \1, \3, \4, \5
Example 2:
If you have a web-based registration system it might be useful to rearrange the order data into a format easily used by a database:
name = John Smith
address1 = 385 Central Ave.
address2 =
city = Cincinnati
state = OH
zip = 45238
into:
John Smith, 385 Central Ave.,, Cincinnati, OH, 45238,
This can be done with the following expression:
UltraEdit RegExp:
Find What: name = ^([a-z ]+^)^paddress1 = ^([a-z 0-9.,]+^)^paddress2 = ^([a-z 0-9.,]++^)^pcity = ^([a-z]+^)^pstate = ^([a-z]+^)^pzip = ^([0-9^-]+^)
Replace With:^1, ^2, ^3, ^4, ^5, ^6
UNIX RegExp:
Find What: name = ([a-z ]+)\paddress1 = ([a-z 0-9.,]+)\paddress2 = ([a-z 0-9.,]*)\pcity = ([a-z]+)\pstate = ([a-z]+)\pzip = ([0-9^-]+)
Replace With:\1, \2, \3, \4, \5, \6