Forum > General

Basic Strings, Fresh eyes please.

<< < (3/3)

eny:
You're entitled to your opinion of course, as is marcov.

RE's are a powerful mechanism to solve many PITA parsing problems (like the one commodianus posted). In a "Pascal" or any other environment.
I'm simply showing another more robust, flexible, professional and future proof way to solve the given problem.
What you do with the information is up to you.
I don't intend to start a religious war about the pros and cons of regular expressions.

Marc:
Regexes are a powerful tool to match on patterns. I use them myself too. And for the given case they might be OK.
But that doesn't mean they are easy to maintain or to debug due to its concatenation of gibberish.

Before you know you might end up with something like this (incorrect and should be on one line)
--- Code: ---(([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6})|
(([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5})|
(([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4})|
(([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3})|
(([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2})|
(([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1})|
((([0-9a-f]{1,4}:){1,7}|:):)|
(:(:[0-9a-f]{1,4}){1,7})|
(((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}))|
((([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}))|
(([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
(([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
(([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
(([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
(([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
((([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|
(:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})

--- End code ---

theo:

--- Quote from: eny on April 03, 2010, 05:17:15 pm ---RE's are a powerful mechanism to solve many PITA parsing problems (like the one commodianus posted). In a "Pascal" or any other environment.

--- End quote ---

I use them at times when writing PHP code.
But I'm looking for faster and better readable solutions first when writing Pascal code.

eny:
Of course RE's should be used when appropriate.
Marc's example perfectly shows how not to use RE's.

@theo: If I look at this code:

--- Code: ---    ArtName := RightStr(CurArt, Length(CurArt) - Pos('">', CurArt)-1);
    ArtName := LeftStr(ArtName, Pos('</a>', ArtName)-1);   

--- End code ---
I have to read it a couple of times to understand what really happens. It's the same as with Italian: I can read it but I don't understand a word of it! RE's are an elegant way to get rid of this textparsing code.
Less code means fewer chances for mistakes and errors and lower maintenance costs.

theo:

--- Quote from: eny on April 03, 2010, 06:48:18 pm ---@theo: If I look at this code:

--- End quote ---

I'd do this job like this. It looks understandable, reusable and clean to me:


--- Code: ---uses strutils;

function ExtractBetween(AString,StartS,EndS:String):String;
var Pos1,Pos2:integer;
begin
 Result:='';
 Pos1:=Pos(StartS,ASTring);
 if Pos1> 0 then
 begin
  Pos2:=PosEx(Ends,ASTring,Pos1);
  if Pos2>0 then
   Result:=Copy(AString,Pos1+Length(StartS),Pos2-Pos1-Length(StartS));
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Caption:=ExtractBetween('<li><a href="index.cfm?id=37237">ZOCE, OUR LADY OF</a></li>','">','</a>');
end;     
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version