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
Solved

Regexp replace from bracketed ascii number to the corresponding char

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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