How to use regular expressions in Delphi

xiaoxiao2021-03-06  78

Regular expressions were first proposed by mathematician Stephen Klene in 1956. He is proposed on the basis of the increasing research results of natural language. Regular expressions with full syntax use in terms of format matching of characters, later applied to the field of melting information technology. Regular expressions are not a dedicated language, but it can be used to find and replace text in a file or character. Regular expressions were used in many programs, but Delphi, which was one of my common programming tools, did not directly provide support for regular expressions. Under depressed, after searching for online, the following methods are found in Delphi using regular expressions. For the sake of clear explanation, we will describe the following examples: HTTP: //www.xcolor.cn/page1.htm Seeking: The correct answer in the link in the link is: Page1.htm

Method 1 Use Microsoft ScriptControl controls

1. Write a script file (TEST.VBS), which contains the regular expression function to use.

Function GetURLFile (URL)

Set regObject = new regexp

With regobject

.Pattern = "/ w /. / W (?!.)"

.IGNORECASE = TRUE

.Global = true

End with

SET MATCHS =

RegObject.execute (URL)

IF matchs.count> 0 THEN

For Each Mach in matchs

GetURLFile = Mach.Value

NEXT

END IF

Set regObject = Nothingend function

2. Download the latest version of "Microsoft (R) Windows (R) Script"

You can find the download in the following address.

3. Install Microsoft (R) Windows (R) Script

Visual Basic (R) Script Edition (VBScript.) Version 5.6, JScript (R) Version 5.6, Windows Script Components, Windows Script Host 5.6, Windows Script Runtime Version 5.6. Will be installed into your system

4. Import MSScript.ocx in Delphi to generate TScriptControl controls

5. Call TScriptControl using the following code

Procedure TForm1.Button2Click (Sender: TOBJECT); VARA: Olevariant; Begin

Memo2.Lines.LoadFromfile ('Test.vbs');

ScriptControl1.language: = 'vbscript'; ScriptControl1.Addcode (String (Memo2.Text)); A: = VararrayCreate ([0, 0], Varvariant); a [0]: = 'http: //www.xolor. CN / PAGE1.HTM '; Memo1.Lines.Add (CallFunction (' geturlfile ', a));

END;

Function TFORM1.CALLFunction (const params: olevariant): olevariant; VarsaRray: PsafeArray; Begin

TRY / / Translate into Safe Arun Sarray: = PsafeArray (Tvardata (Params) .vaRray); // Call function Result: = scriptcontrol1.run (functionname, sarray); Excepton E: Exception DobeGinend; End;

Method 2 Microsoft Regexp

1. Download and install the latest version of "Microsoft (R) Windows (R) Script"

2. Regexp is included in VBScript.dll so we must first register regr32 vbscript.dll

Note (the default already contains the control after installation of IE5)

3. Introduce "Microsoft Vbscript Regular Express" in Delphi

Main Menu -> Project-> Import Type library-> Select "Microsoft Vbscript Regular Expressions" Generate TREGEXP Controls in the list

4. Call the TREGEXP control using the following code

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); VAR

Machs: imatchcollection; matchs: match; submatch: isubmatches; i, j: integer; beginRegexp1.global: = true; regexp1.pattern: = '/w /w (?!.)';Regexp1.ignoreCase: = true Machs: = regexp1.execute ('http://www.xcolor.cn/dd/page1.htm') asimatchcollection; for i: = 0 to machine.count - 1 dobeginmatchs: = machines.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;

转载请注明原文地址:https://www.9cbs.com/read-120735.html

New Post(0)