• Delphi XE 是第一個Delphi內建regular expressions的版本。 (等好久了…)

 

  • 在大部分的case中,只要use RegularExpressions unit 即可使用。

 

  • RegularExpressions跟.net中system.text.regularexpressions 用法相似。

 

 

  • 支援的符號及用法說明 (因符號眾多,僅列重點常用)
符號 說明 pattern 測試及結果
^

必須從字串開頭或行首來開始比對。

^\d{3}

"901-333-" 中的
"901"

$

比對必須發生在字串結尾,或發生在行尾或字串結尾的 \n 之前。

-\d{3}$

"-901-333" 中的
"-333"

.

萬用字元:比對除 \n 以外的任何單一字元。

a.e

"nave" 中的 "ave"
"water" 中的 "ate"

*

比對上一個項目零次或多次。

\d*\.\d

".0" -->".0"
"19.9" -->"19.9"
"219.9" -->"219.9"

+

比對上一個項目一次或多次。

"be+"

"been" 中的 "bee"、
"bent" 中的 "be"都符合

?

比對上一個項目零次或一次。

"rai?n"

"ran"、"rain"都符合

{n}

比對上一個項目剛好 n 次。

",\d{3}"

"1,043.6" 中的 ",043"、
"9,876,543,210" 中的 ",876"、",543" 和 ",210"

{n,}

比對上一個項目至少 n 次。

"\d{2,}"

"166"、"29"、"1930"

{n,m} 比對上一個項目至少 n 次,但不超過 m次。 "\d{3,5}"

"166"、"17668"、"193024" 中的 "19302"

[^]

否定字元:比對不在 character_group 中的任何單一字元。 根據預設,character_group 中的字元會區分大小寫。

[^aei]

輸入"reign" ,取出符合的 "r"、"g"、"n"

[-]

字元範圍:比對從 first 至 last 的範圍內的任何單一字元。

[A-Z]

"AB123" 中的 "A"、"B"

官方網頁: http://docwiki.embarcadero.com/RADStudio/en/Regular_Expressions
msdn說明:http://msdn.microsoft.com/zh-tw/library/az24scfc.aspx 

  • 做了一些範例:
    範例函數:replace,IsMatch,match,matches,split,Escape
uses RegularExpressions;

{replace:覆蓋指定條件的字串}
procedure TForm1.Button1Click(Sender: TObject);
var
  sStr: string;
  sPattern: string;
  sReplace: string;
begin
  { replace: 覆蓋符合條件的字串,為指定字串}
  sPattern := '^the';
  sStr := 'the email is the...';
  sReplace := 'The';
  ShowMessage(TRegEx.replace(sStr, sPattern, sReplace));
  // result => The email is the
end;

{IsMatch: 測試是否有符合條件的字串}
procedure TForm1.Button2Click(Sender: TObject);
var
  sStr : string;
  sPattern :string;
begin
  sPattern:='^[A-Z]';    //規則=字串開頭是大寫英文字母
  sStr :='D121234567';

  if TRegEx.IsMatch(sStr, sPattern) then
    ShowMessage('match')
  else ShowMessage('not match');

  sStr :='9121234567';
  if TRegEx.IsMatch(sStr, sPattern) then
    ShowMessage('match')
  else ShowMessage('not match');
end;

{match: 可取出 符合規則的每個字串}
procedure TForm1.Button3Click(Sender: TObject);
var
  sStr: string;
  sPattern: string;
  m: TMatch;
begin
  { match: 可取出 符合規則的每個字串 }
  sPattern := '[A-Z]';
  sStr := 'A1B1C34567D';
  m := TRegEx.match(sStr, sPattern);
  while (m.Success) do begin
    ShowMessage('Found:' + m.Value + ' index:' + inttostr(m.Index));
    // 依序顯示A B C D 及位置
    m := m.NextMatch;
  end;
end;

{matchs: 可取出 符合規則的每個字串,match也可以,
         差別在執行 matches後已經將結果先存到TMatchCollection內 }
procedure TForm1.Button4Click(Sender: TObject);
var
  sStr: string;
  sPattern: string;
  matchs: TMatchCollection;
  m: TMatch;
  i: Integer;
begin
  sPattern := '[A-Z]';
  sStr := 'A1B1C34567D';

  matchs := TRegEx.Matches(sStr, sPattern);

  {兩種取出值的方法}
  Memo1.Lines.Add('[First method]----------');
  Memo1.Clear;
  for m in matchs do
  begin
    Memo1.Lines.Add(m.Value);
  end;

  Memo1.Lines.Add('[Second method]----------');
  for i := 0 to matchs.Count - 1 do
  begin
    Memo1.Lines.Add(matchs[i].Value);
  end;

end;

{Split: 指定分隔字元,取出分割後的字串 }
procedure TForm1.Button5Click(Sender: TObject);
var
  sStr: string;
  sPattern: string;
  AStr: TArray;
  sResult: string;
begin
  sPattern := '[;, ]'; // 遇到; 或, 或空白就分割
  sStr := 'John mary;Tom joe;charles,jessica';
  AStr := TRegEx.Split(sStr, sPattern);

  Memo1.Clear;
  for sResult in AStr do
  begin
    Memo1.Lines.Add(sResult); // 依序顯示 John mary Tom joe charles jessica
  end;
end;

{假設你正規化條件中有使用Escape 就必須用 Escape()包住去使用
 Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space)
 by replacing them with their escape codes.
 http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx}
procedure TForm1.Button6Click(Sender: TObject);
var
  sStr: string;
  sPattern: string;
  m: TMatch;
begin
  { 想找出 字串中被[]包住,傳回含[及]的字串
    正規化應該是寫這樣==> sPattern := '[(.*?)]';
    但[]是正規化的關鍵字,使用Escape('[') 拆散[]即可.
  }
  sStr := 'The animal [what kind?] was visible [by whom?] from the window.';

  sPattern := TRegEx.Escape('[') + '(.*?)]'; // 本應正確的正規化'[(.*?)]',但有[這個Escape

  m := TRegEx.match(sStr, sPattern);
  while (m.Success) do // 或用一句? if TRegEx.Match(txt, pattern).Success then
  begin
    ShowMessage('Found:' + m.Value + ' index:' + inttostr(m.Index));
    // 依序顯示A B C D 及位置
    m := m.NextMatch;
  end;
end;
參考網址:
http://www.cnblogs.com/del/archive/2011/03/28/1998100.html
arrow
arrow

    味味A 發表在 痞客邦 留言(0) 人氣()