UltraEdit - 正则表达

最近做DT数据,很多都是重复劳作,需要更高效和准确来完成,找了这些,已备后用

Regular Expressions are essentially patterns rather than specific strings that are used with Find/Replace operations. There are many ways that regular expressions may be used to streamline operations and enhance efficiency. We have listed below a reference key for both UltraEdit-style and UNIX-style regular expressions as well as some examples to demonstrate how regular expressions may be used in UltraEdit.

Regular Expressions in UltraEdit
UltraEdit SymbolUNIX SymbolFunction
%^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.
++Matches one or more of the preceding character/expression. At least one occurrence of the character must be found. Does not match repeated newlines.
++*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. See examples below.
[ ][ ]Matches any single character or range in the brackets.
[~xyz][^xyz]A negative character set. Matches any characters NOT between brackets.
^b/fMatches a page break/form feed character.
^p/pMatches a newline (CR/LF) (paragraph) (DOS Files).
^r/rMatches a newline (CR Only) (paragraph) (MAC Files).
^n/nMatches a newline (LF Only) (paragraph) (UNIX Files).
^t/tMatches a tab character.
[0-9]/dMatches a digit character.
[~0-9]/DMatches a non-digit character.
[ ^t^b]/sMatches any white space including space, tab, form feed, etc., but not newline.
[~ ^t^b]/SMatches any non-white space character but not newline.
 /vMatches a vertical tab character.
[a-z_]/wMatches any word character including underscore.
[~a-z_]/WMatches any non-word character.
^{A^}^{B^} (A|B)Matches expression A OR 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/1Numerical 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.


UltraEdit/UNIX Regular Expression Examples


Simple String Matching

Simple string matching is probably the most basic form of regular expressions but can allow you to quickly exploit different patterns so that you can search for more than one string at a time rather than doing multiple Find operations.

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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值