CompareString
说明
根据用于特定“地方”环境的文本对比设置,对两个字串进行比较
返回值
Long,如lpString1<lpString
2,返回1;
如相等,返回2;
如lpString1>lpString2,则返回3。
会设置GetLastError
参数 类型及说明
Locale Long,比较进行时的一个“地方”标识符
dwCmpFlags Long,带 NORM_??? 前缀的一个或多个常数,它们定义了象“忽略大小写”这样的一些选项
lpString1 String,要比较的第一个字串
cchCount1 Long,以字节为单位指定字串长度(如果是DBCS字串,则没有字符)。如设为-1,表示进行自动计算(以NULL中止字符为准)
lpString2 String,要比较的第二个字串
cchCount2 Long,第二个字串的长度(参考 cchCount1 参数说明)
说明
根据用于特定“地方”环境的文本对比设置,对两个字串进行比较
返回值
Long,如lpString1<lpString
2,返回1;
如相等,返回2;
如lpString1>lpString2,则返回3。
会设置GetLastError
参数 类型及说明
Locale Long,比较进行时的一个“地方”标识符
dwCmpFlags Long,带 NORM_??? 前缀的一个或多个常数,它们定义了象“忽略大小写”这样的一些选项
lpString1 String,要比较的第一个字串
cchCount1 Long,以字节为单位指定字串长度(如果是DBCS字串,则没有字符)。如设为-1,表示进行自动计算(以NULL中止字符为准)
lpString2 String,要比较的第二个字串
cchCount2 Long,第二个字串的长度(参考 cchCount1 参数说明)
运行时需要'cp3245mt.dll',Delphi和BCB都有。这里是Delphih5例子。BCB例子也差不多。
unit pcreposix;interfaceuses
windows;
//**************************************************/
//* Perl-Compatible Regular Expressions */
//**************************************************///* Copyright (c) 1998 University of Cambridge */
const REG_ICASE =$01;
const REG_NEWLINE =$02;
const REG_NOTBOL =$04;
const REG_NOTEOL =$08;const REG_ASSERT =1; // internal error ?
const REG_BADBR =2; // invalid repeat counts in {}
const REG_BADPAT =3; // pattern error
const REG_BADRPT =4; // ? * + invalid
const REG_EBRACE =5; // unbalanced {}
const REG_EBRACK =6; // unbalanced []
const REG_ECOLLATE =7; // collation error - not relevant
const REG_ECTYPE =8; // bad class
const REG_EESCAPE =9; // bad escape sequence
const REG_EMPTY =10; // empty expression
const REG_EPAREN =11; // unbalanced ()
const REG_ERANGE =12; // bad range inside []
const REG_ESIZE =13; // expression too big
const REG_ESPACE =14; // failed to get memory
const REG_ESUBREG =15; // bad back reference
const REG_INVARG =16; // bad argument
const REG_NOMATCH =17; // match failedtype
{ The structure representing a compiled regular expression. }
pRegex_t = ^Regex_t;
Regex_t = packed record
re_pcre: pointer;
re_nsub: dword;
re_erroffset: dword;
end; Regmatch_t = packed record
rm_so: Integer;
rm_eo: Integer;
end;{ The functions }{$EXTERNALSYM regcomp}
function regcomp(pReg: PRegex_t; const Pattern: PChar; eflag: Integer): Integer; cdecl;
{$EXTERNALSYM regexec}
function regexec(pRreg: PRegex_t; const Source: PChar; nmatch: DWord; match: array of Regmatch_t; eflag: Integer): Integer; cdecl;
{$EXTERNALSYM regerror}
function regerror(errorno: Integer; const pReg: PRegex_t; errorstr: PChar; error_size: DWord): DWord; cdecl;
{$EXTERNALSYM regfree}
procedure regfree(pReg: PRegex_t); cdecl;implementationconst
cp3245mt = 'cp3245mt.dll';{$EXTERNALSYM regcomp}
function regcomp; external cp3245mt name '_regcomp';
{$EXTERNALSYM regexec}
function regexec; external cp3245mt name '_regexec';
{$EXTERNALSYM regerror}
function regerror; external cp3245mt name '_regerror';
{$EXTERNALSYM regfree}
procedure regfree; external cp3245mt name '_regfree';end.unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;const
pattern = '(?si)<#TPLDEF +(\w*) *>(.*?)<#TPLEND +\1 *>';
type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses pcreposix;{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
preg: regex_t;
pmatch: array [0..9] of regmatch_t;
r,i: integer;
begin
r := regcomp(@preg, PChar(Pattern),0);
if (r=0) then
begin
r := regexec(@preg, PChar(Memo1.Lines.Text),9,pmatch,0);
if (r=0) then
begin
for i := 0 to 2 do
begin
if(pmatch[i].rm_eo - pmatch[i].rm_so > 0) then
begin
Memo2.Lines.Add(':'+InttoStr(i)+':'+Copy(Memo1.Lines.Text,pmatch[i].rm_so+1,pmatch[i].rm_eo-pmatch[i].rm_so));
end;
end;
end;
end;
regfree(@preg);end;end.*******DFM************object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Edit1: TEdit
Left = 56
Top = 24
Width = 249
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object Memo1: TMemo
Left = 56
Top = 56
Width = 321
Height = 105
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object Memo2: TMemo
Left = 56
Top = 176
Width = 321
Height = 153
Lines.Strings = (
'Memo2')
TabOrder = 2
end
object Button1: TButton
Left = 392
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 3
OnClick = Button1Click
end
end
以下是从UltraEdit中抽出来的关于正则表达式的描述,与本例中的可能个别地方不太一样,具体真正的帮助,C++Builder5的Help文件中有清楚的说明。Regular Expressions (Unix Syntax):Symbol Function
\ Marks the next character as a special character. "n" matches the character "n". "\n" matches a linefeed or newline character.
^ Matches/anchors the beginning of line.
$ Matches/anchors the end of line.
* Matches the preceding character zero or more times.
+ Matches the preceding character one or more times.
. Matches any single character except a newline character.
(expression) 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.The corresponding replacement expression is \x, for x in the range 1-9. Example: If (h.*o) (f.*s) matches "hello folks", \2 \1 would replace it with "folks hello".
[xyz] A character set. Matches any characters between brackets.
[^xyz] A negative character set. Matches any characters NOT between brackets.
\d Matches a digit character. Equivalent to [0-9].
\D Matches a nondigit character. Equivalent to [^0-9].
\f Matches a form-feed character.
\n Matches a linefeed character.
\r Matches a carriage return character.
\s Matches any white space including space, tab, form-feed, etc but not newline.
\S Matches any nonwhite space character but not newline.
\t Matches a tab character.
\v Matches a vertical tab character.
\w Matches any word character including underscore.
\W Matches any nonword character.
Note - ^ refers to the character '^' NOT Control Key + value.Examples:
m.n matches "man", "men", "min" but not "moon". Te+st matches "test", "teest", "teeeest" etc. BUT NOT "tst". Te*st matches "test", "teest", "teeeest" etc. AND "tst". [aeiou] matches every lowercase vowel
[,.?] matches a literal ",", "." or "?".
[0-9, a-z] matches any digit, or lowercase letter
[^0-9] matches any character except a digit (^ means NOT the following)You may search for an expression A or B as follow:"(John|Tom)"
This will search for an occurrence of John or 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:"(John|Tom)(Smith|Jones)"
This will search for John or Tom followed by Smith or Jones.Additionally:\p Matches CR/LF (same as \r\n) to match a DOS line terminatorIf Regular Expression is not selected for the find/replace and in the Replace field the following special characters are also valid:Symbol Function
^^ Matches a "^" character
^s Is substituted with the selected (highlighted) text of the active file window.
^c Is substituted with the contents of the clipboard.
^b Matches a page break
^p Matches a newline(paragraph) (CR/LF).
^t Matches a tab characterNote - ^ refers to the character '^' NOT Control Key + value.