在Delphi程式中使用Regex

來源:互聯網
上載者:User

TRegexp的Regex的格式說明文檔

.  代表所有字元,分行符號號(newline)除外
*  0或多次 *號會儘可能多匹配
+  1或多次 +號會儘可能的多匹配
?  0或1個,即使前面有+號或*號,也只能為2個(即1+1=2個)
^  否定符,如 [^2]+ 匹配不能含有2的任一字元串
^ 匹配開頭  ^D,匹配以D開頭的字串
$ 匹配結尾  $S,匹配以S結尾的字串
| 相當於or About cats and dogs cat|dog|mouse|fish 匹配cat(第一個,如果第一不存在,匹配第二個
[] 取其中一個(1次)
() 匹配操作模組 取值用/1  /2等表示  Set(Value)? 匹配 Set或者SetValue. (?:Value)前面用?:則不提取匹配的值,如果不需要取值就這樣用,效率會高
{min,max} 重複的次數 {min,}不少於min個到無窮個,{num}精確到Num個
/d 匹配所有阿拉伯數字
/b 匹配僅僅是文字的字串,2個位元組寬的文字,如中文,日文等 
/B 和/b剛好想反,只匹配1位元組寬的如字母,數字,不匹配符號
/w 匹配word character,也會匹配阿拉伯數字(匹配1次)sd35fg3 /b3/b 匹配的是d35
/p{L} 匹配Unicode的字元
範圍查詢
q(?=u) 匹配question,不匹配Iraq,http://www.regular-expressions.info/quickstart.html
q(?!u) 不匹配question,匹配Iraq,http://www.regular-expressions.info/quickstart.html
(?<=a)b 匹配abc
(?<!a)b 不匹配abc

 

======================================

 

{
使用微軟RegExp
1. 下載並安裝最新版的"Microsoft(r) Windows(r) Script"
2. RegExp包含在vbscript.dll中所以我們必須先註冊regsvr32 vbscript.dll
注(安裝了Ie5後預設已經包含該控制項)

3.在Delphi中引入"Microsoft VBScript Regular Expressions"
主菜單->Project->Import type library->在列表中選擇"Microsoft VBScript Regular Expressions"

產生TRegExp控制項

4.使用以下代碼調用TRegExp控制項
}
procedure TForm1.Button1Click(Sender: TObject);
var
matchCollection: IMatchCollection;
Matchs: Match;
submatch: ISubMatches;
i, j: integer;
begin
  RegExp1.Global := true;
  RegExp1.Pattern := '/w+/./w+(?!.)';
  RegExp1.IgnoreCase := true;
  matchCollection := RegExp1.Execute(edit1.Text) as IMatchCollection;

  for i := 0 to matchCollection.Count - 1 do
  begin
    Matchs := matchCollection.Item[i] as Match;
    submatch := Matchs.SubMatches as ISubMatches;
    memo1.Lines.Add(Matchs.Value);
    for j:=0 to submatch.Count -1 do
      memo1.Lines.Add(submatch.Item[j])
  end;
end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.