Solved

Regexp replace from bracketed ascii number to the corresponding char

Posted on 2014-01-07
3
481 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 75
Finding (and replacing) text between two strings in Php 7 52
Grep - regex - find files matching pattern 2 45
Problem to picture file 20 34
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now