Solved

Regexp replace from bracketed ascii number to the corresponding char

Posted on 2014-01-07
3
484 Views
Last Modified: 2014-01-09
I would like to replace a string
"<2>Test<3><13><10>"

to

Chr(2)+"Test"+Chr(3)+Chr(13)+Chr(10)

String length = 1+4+1+1+1 = 8

How can I do this with a RegEx replace? (TRegExReplace in Delphi)
0
Comment
Question by:lefodnes
3 Comments
 
LVL 17

Assisted Solution

by:Kent Dyer
Kent Dyer earned 50 total points
ID: 39761568
0
 

Author Comment

by:lefodnes
ID: 39761709
Thanks, yes. I would make it using a function, so now I probably need to figure out how to do it for TRegExReplace in Delphi (Pascal).
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 450 total points
ID: 39766628
whith TRegEx do this:

uses System.RegularExpressions;

function TForm1.ReplaceMyStr(const Match: TMatch): String;
var
  s: String;
  i: Integer;
begin
  Result := '';
  if Match.Success then
  begin
    s := Copy(Match.Value, 2, Match.Length-2); //exclude brackets
    if Length(s) > 0 then
    begin
      if TryStrToInt(s, i) then
        Result := Chr(i);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  regex: TRegEx;
  input: string;
  myEval: TMatchEvaluator;
begin
  iCC := 0;
  input := '<2>Test<3><13><10>';
  regex.Create('');
  myEval := ReplaceMyStr;
  Label2.Caption := regex.Replace(input, '<[^>]+>', myEval);
end;

Open in new window


...but maybe can be easier with stringreplace:
s :=  '<2>Test<3><13><10>';
s := StringReplace(s, '<2>', Chr(2), [rfReplaceall]);
s := StringReplace(s, '<3>', Chr(3), [rfReplaceall]);
s := StringReplace(s, '<10>', Chr(10), [rfReplaceall]);
s := StringReplace(s, '<13>', Chr(13), [rfReplaceall]);

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 120
Work with App store 7 52
Math Equation 23 86
How come this regular expression allows "e"? 14 38
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question