Re: sample code from TPerlRegEx?



Here is a method (ParseNaturalListings) from a class I made (TgbGoogle) to pull web addresses from a google search results page:


procedure TgbGoogle.ParseNaturalListings;
begin
RegEx.Regex := '(?i)href="HTTP://(www\.)?([_a-z\d\-]+(\.[_a-z\d\-]+)?)/?"';
RegEx.Subject := ResponseHTML;
if RegEx.Match then
repeat
Kind := 1;
ResultHostName := LowerCase(RegEx.SubExpressions[2]);
AddSearchTermAreaResult;
until not RegEx.MatchAgain;
end;

RegEx is a property of type TPerlRegEx. Its Regex property gets assigned the regular expression string, and its subject takes on the value of an entire google results page represented as a string. Its Match method is called to populate the ResultHostName property with the result of the second subexpression. This continues until there are no more matches.

Since I suck at writing regular expressions, I use RegEx Buddy to help.

I hope this example helps you along. In your example, I think you might use the TFileStream class to load the entire file into a stream, then, transfer the contents of the stream to a string variable to use as the regex's Subject property.

Goog




"Deli Soetiawan" <crushfire2004@xxxxxxxxx> wrote in message news:op.uetpwlv8r9wvmw@xxxxxxxxxxxxxx
On Thu, 24 Jul 2008 21:22:13 +0700, Goog <goog@xxxxxxxx> wrote:

Have a look at
http://www.regexbuddy.com/
It's a superb product that should help you get your job done.

what i mean was actual code for regex from file, maybe something like:

AssignFile(Fn, 'test.dat');
Reset(Fn);
While not Eof(Fn) do
begin
ReadLn(Fn, Data);
PerlRegEx.Subject := Data;
PerlRegEx.Match;
ListBox.Items.Add(PerlRegEx.MatchedExpression);
end;
CloseFile(Fn);

yeah something like reading all files and pull out match pattern from it, i can do it with readln but it was to slow,
i can do with BlockRead but since the buffer was a array (like array[1..512] of char) the regex can't get much match, if only anyone have sample with FileStream or TStream it would be nice (kinda hard to find reference about it in Win32, all i got was reference for .NET)

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

.



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)
  • Re: Trim a multiple line message to a single line
    ... You can do this quite easily with either a regex or a simple function I'll try to demonstrate both: ... private string LayoutInput ... Could you send a sample file with two of these data blocks f what ...
    (microsoft.public.dotnet.languages.csharp)