Recent

Author Topic: How to extract string  (Read 12567 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to extract string
« Reply #15 on: April 16, 2014, 03:56:04 pm »
The question now is, is there is another way than this? Maybe a simpler, better, or optimized way?

There are always many ways to approach tasks such as parsing/validation. Some are conceptually simpler than others (using linear arrays rather than tree-based data structures, for instance). Likewise someone can almost always suggest how a routine could be optimised.
But "better" depends on so many factors apart from simplicity and raw speed. After all, does it matter if a complex routine takes 40ms rather than 4ms, particularly if the slower routine is easier to debug and maintain? Maintainability, adaptability for dealing with unusual characters, ease of reuse in other projects, robustness if extended to add new functionality etc., security in leaving cleaned (or randomised) memory behind afterwards ... These and other factors are often more relevant for the long-term quality of software.

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
Re: How to extract string
« Reply #16 on: April 16, 2014, 04:32:29 pm »
But "better" depends on so many factors apart from simplicity and raw speed. After all, does it matter if a complex routine takes 40ms rather than 4ms, particularly if the slower routine is easier to debug and maintain? Maintainability, adaptability for dealing with unusual characters, ease of reuse in other projects, robustness if extended to add new functionality etc., security in leaving cleaned (or randomised) memory behind afterwards ... These and other factors are often more relevant for the long-term quality of software.

Yes, I agree that faster is not always better.
But of course if too much slower is not better too.
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to extract string
« Reply #17 on: April 16, 2014, 11:38:39 pm »
@Fahmy,
I enjoyed reading your code and decided to do similar steps using regular expressions. You might want to try it. Replace your ParseText with this one:
Code: [Select]
uses
.., regexpr;

function ParseText(const FLabel, FValue: TStrings; const Text, Format: string): boolean;
var
  r: TRegExpr;
  i: Integer;
  K: String;
  EscapedFormat: String;
begin
  r := TRegExpr.Create;

  //Extract labels from <SomeLabel>
  r.Expression := '<([^>]+)>';
  r.InputString := Format;
  Result := r.Exec(1);
  if Result then
  begin
    FLabel.Add(r.Match[1]);
    while r.ExecNext do
      FLabel.Add(r.Match[1]);
  end
  else
  begin
    r.Free;
    Exit;
  end;

  //Escape RE chars used in Format
  EscapedFormat := QuoteRegExprMetaChars(Format);

  //Replace <> with (.+)
  r.Expression:='(<[^>]+>)';
  K := r.Replace(EscapedFormat, '(.+)', False);
  EscapedFormat := K;

  //Replace * with .+
  r.Expression:='\\\*';
  K := r.Replace(EscapedFormat, '.+', False);

  //Apply it on Text
  r.Expression := K;
  r.InputString := Text;
  Result := r.Exec(1);
  if Result then
  begin
    for i := 1 to r.SubExprMatchCount do
      FValue.Add(r.Match[i]);
  end;

  r.Free;
end;
« Last Edit: April 17, 2014, 12:12:00 am by engkin »

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
Re: How to extract string
« Reply #18 on: April 17, 2014, 04:11:08 pm »
@enkin,
Thank you, your code working great. Even though currently I still trying to understand what's going on that code because I never use regular expression before. :D But I try to learn this regexpr since its seems very useful. :)
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

 

TinyPortal © 2005-2018