Something to remember when working with groups is that a match's Groups collection always returns the entire match as group 0, so the groups from your expression start at 1. You could also call TRegEx.Matches, this returns a TMatchCollection which supports enumeration (using the for in construct), e.g : The same applies to Match.NextMatch, this makes it easy to iterate the matches. The match method will always return a TMatch, even when no match is found, so you should check the Success property of the Match to determine if a match is found. In the above example, we are trying to extract the word before the "world", and capturing that in a group. group 0 is the entire match, so count will always be at least 1 for a match create our regex instance, and we want to do a case insensitive search, in multiline mode Procedure FindSomething(const searchMe : string) So lets look at how we might use TRegEx to find some text in a string. The static versions of the methods are provided for convenience, and should only be used for one off matches, if you are matching in a loop or repeating the same search often then you should create an 'instance' of the TRegEx record and use the non static methods. TRegEx is a record with a bunch of methods and static class methods for matching with regular expressions. The main type in RegularExpressions.pas is TRegEx. RegularExpressions.pas is what you will use in your code. This unit is based on code we currently use in FinalBuilder 6 & 7, it's well tested and has proven to be very reliable in our products. The XE interface to pcre is a layer of units based on contributions from various people, the pcre api header translations in RegularExpressionsAPI.pas (Florent Ouchet and co), the wrapper class TPerlRegEx (Jan Goyvaerts) in RegularExpressionsCore.pas and the record wrappers on RegularExpressions.pas (myself). Users of earlier versions of delphi can use it with TPerlRegEx, a delphi class wrapper around it. It's a fast and compliant (with generally accepted regex syntax) engine which has been around for many years. The regular expression engine in Delphi XE is PCRE (Perl Compatible Regular Expression). I'm assuming you already understand regular expressions and the associated terminology, if not take a look here for some tutorials etc. In this blog post I'll show some basic usage of regular expressions in delphi. To use this, create a form with a button,ĭouble click the button (to open the source code)Īuthor: Robert Clemenzi URL: cpcug.Delphi XE includes Regular Expression support, something that has been requested many times over the years. TempStr := tempStr + crlf + crlf + ' ' + 'c:\temp' įindClose(SearchRec) // This is required to free memoryįunction FileExists(FileName: string): Boolean įile copy - from the Eof, Read, Write example. TempStr := 'A file with the following name already exists.' TempStr := 'The following directory already exists.' If (SearchRec.Attr And faDirectory) 0 Then If FileFound = 0 then // a match was found The more complicated FindFirst command must be usedīecause FileExists does not find directories.Ĭrlf = Chr(13) + Chr(10) // Carriage return & LinefeedįileFound := FindFirst('c:\temp', faAnyFile, SearchRec) (* File type declarations from Delphi file help *)įiles and directories may not have the same names. Set Current Location Seek(var F N: Longint) Write to File FileWrite(Handle: Integer const Buffer Count: Integer): Integer Get File Mode F.Mode or (F as TFileRec).Mode Test Directory existance if DirectoryExists('c:\temp') thenĪdjustLinesBreaks(const S: string): string - adjustsįileOpen(const FileName: string Mode: Integer): Integer Test File existance if FileExists('c:\filename.tst') then Set File Date/Time FileSetDate(Handle, Age) Get File Date/Time tempDate := FileGetDate(Handle) Set File Attributes FileSetAttr('c:\filename.tst', faReadOnly) Get File Attributes temp = FileGetAttr('c:\filename.tst') T_int := FindFirst ('c:\*.tst', attr, F) Ĭurrent Drirectory t_str := GetCurrentDir Search for File t_str := FileSearch('readme.txt', 'c:\ c:\windows') Rename a File RenameFile('test.txt', 'string.tst')ĭelete a File DeleteFile('c:\*.tst') - Don't know if wildcards are allowed Have different commands for many of theseĪnd System / I/O routines Change Directory SetCurrentDir('c:\path') Testing for the Existance of a Directoryįile Manager Commands SysUtils/FileOpen and System/AssignFile.These are low-level file access routines.įor normal file operations use the normal AssignFile, Rewrite, These routines map to the Windows API functions and return file handles, Use of the non-native Pascal file variable handlers such as FileOpen is discouraged. The delphi help files suggest that you use the Pascal routines if you can. There are 3 basic methods to perform File I/O File I/O - Delphi Examples File I/O - Delphi Examples
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |