Regular Expression usage (PerlRegEx)

Source: Internet
Author: User
Delphi and Regular Expressions

Bytes -------------------------------------------------------------------------------------------

Regular expressions are used in Delphi. Currently, PerlRegEx is the first choice. You must thoroughly and carefully study it.

Http://www.regular-expressions.info/delphi.html
Download: http://www.regular-expressions.info/download/TPerlRegEx.zip

Installation Method:

1. Put the decompressed TPerlRegEx folder in a proper place. I put it in the Imports directory of Delphi.

2. Currently, the latest For Win32 version is applicable to Delphi 2006 and 2007.
Open PerlRegExD2006.dpk and prompt that the resource file is missing;
Right-click PerlRegExD2006.bpl in the Project Manager window and execute Install;
At this time, TPerlRegEx is already available in the Tool Palette list, in the JGsoft group.

3, Tools-> Options-> Environment Options-> Delphi Options-> Library-Win32-> Library path->
Add path:... \ Imports \ TPerlRegEx

4. You can use it! You can directly add uses PerlRegEx or Tool Palette.
If you do not like the method of adding Tool Palette, skip step 2.

Planned learning steps: 1. Regular syntax; 2. TPerlRegEx function.

Character replacement

// Test usesPerlRegEx; // uses regular expression unit procedure TForm1.FormCreate (Sender: TObject); varreg: TPerlRegEx; // declare the regular expression variable beginreg: = TPerlRegEx. create (nil); // Create reg. subject: = 'sss'; // This is the source string to be replaced by reg. regEx: = 's'; // This is an expression. Here it is the substring reg to be replaced. replacement: = '◆ '; // The New reg string to be replaced. replaceAll; // execute all replace ShowMessage (reg. subject); // return the replacement result: ◆ S ◆ SFreeAndNil (reg); // The nil is not used because the owner gave the nil at the time of creation. freeend;
  

Use absolute match of strings

 

Procedure TForm1.Button1Click (Sender: TObject );
Begin
With PerlRegEx1 do begin
RegEx: = Edit1.Text;
Subject: = Edit2.Text; // match the native RegEx and Subject
If Match then ShowMessage ('match') else ShowMessage ('not Match ');
End;
End;

String replacement usage
------------------------
With PerlRegEx1 do begin
Subject: = 'bill loves Hillary ';
RegEx: = 'hillary ';
Replacement: = 'monica ';
If Match then Replace;
ShowMessage (Subject); // "Bill loves Monica"
End;
-------------------------------------------------
Arbitrary match
----------------
With PerlRegEx1 do begin
Subject: = 'linux ';
RegEx: = 'windows | Linux '; // Matches 'windows' or 'linux', whichever comes first
If Match then Label1.Caption: = MatchedExpression + 'rule-z! ';
End;
-------------------
Option-> CaseLess-> True-> case insensitive; otherwise, case sensitive
_____________________________________________________
Character matching
-------------
With PerlRegEx1 do begin
Subject: = 'rating: aaa ';
RegEx: = 'a + '; // Matches the first occurence of 'A', including any A's that may follow right after it
If Match then Label1.Caption: =
'You have been rated with '+ IntToStr (MatchedExpressionLength) + 'A' s'
Else
Label1.Caption: = 'you have a poor rating ';
End;
If expression A appears, the match is successful!
______________________________
Obtain the string before the matching string.
----------------------------------
With PerlRegEx1 do begin
Subject: = 'haha Bye Hei hei ';
RegEx: = 'bye ';
If Match then Label1.Caption: = 'before typing "Bye", you typed: '+
Copy (Subject, 1, MatchedExpressionOffset-1 );
End;
--------------------------------
Match the first character in uppercase
---------------------------------

With PerlRegEx1 do begin

Subject: = 'I like to hang out at Foo bar ';

RegEx: = '([A-Za-z] +) bar'; // ([A-Za-z] +) indicates that the first character is capitalized

Replacement: = '\ 1 is the name of the bar I like ';

If Match then Replace;

ShowMessage (Subject );

End;

Split string (split a string by specified separator)
--------------------------------- Add text ------
One, Two, Three, Four
You, me, him
You, us, they
----------------------------------------------
Procedure TForm1.Button2Click (Sender: TObject );
Begin
PerlRegEx1.Subject: = Memo1.Lines. GetText;
PerlRegEx1.RegEx: = ',';
PerlRegEx1.Split (Memo2.Lines, 0 );
End;
----- After split, each substring occupies a line -------------
One
Two
Three
Four
You
Me
He
You guys
We
They

---------- Code -----------------
Begin
PerlRegEx1.Subject: = Memo1.Lines. GetText;
PerlRegEx1.RegEx: = ',';
PerlRegEx1.Split (Memo2.Lines, 0); // 0 indicates all substrings in the source string
End;
----------------------------------
This function is recommended for Text Databases-> split the text and store it in the data table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.