Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regexp replace from bracketed ascii number to the corresponding char

Posted on 2014-01-07
3
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Assisted Solution

by:Kent Dyer
Kent Dyer earned 200 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 27

Accepted Solution

by:
Sinisa Vuk earned 1800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

705 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