Solved

Regexp replace from bracketed ascii number to the corresponding char

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
NotAlone Challenge 20 72
SQL400 max size 5 54
c# regex: extract & replace text between braces 6 28
Regex to filter only IP's? 4 25
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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…

757 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

16 Experts available now in Live!

Get 1:1 Help Now