Jump to content
Bob Baudewyns

Smart characters editing in strings in Delphi

Recommended Posts

I need to update a large number of strings because the syntax is not compliant with the expected format. I would like to automate that process.

Such strings can have a mix of alphanumeric, numeric and special characters. A small example would be appropriate here

Let's say 3 different strings:

NameA 01 01 [A-01]
NameB 01 02 [B-02]
NameC 01 03 [C-03]

Should become:

My Name A [1963-A-01]
My Name B [1963-B-02]
My Name C [1963-C-03]

As you can see:

 - 'My ' is inserted at the beginning
 - A space is inserted between 'Name' and 'A'
 - The two groups of numbers before '[' are removed
 - '1963-' is inserted after '['

Rules:

  • String Length can be different
  • Some characters need to be added/inserted
  • Some others need to be removed
  • Others need to be replaced
  • Inserting space/special characters

 

The same rules must be applied to all strings based on a specific "syntax" defining what should be added/removed/replaced and at which location in the string.

 

Do you known the most efficient way to achieve such smart replacement with Delphi ?

Is Regular Expression can be applied here and how ?

 

I'm looking to provide an Edit field that would allow the user to enter the syntax(rules) and see the result applied dynamically like done by Regular Expression web sites

 

 

 

Share this post


Link to post

Yes use REs here. Something like

Search: ^Name(.+) .+ [(.+)]$
Replace: My Name $1 [1963-$2]

(typed right from my head, bugs possible)

Share this post


Link to post

Hi,

Thank you for your feedback.

 

@Fr0sT.Brutal.

I am not at all a RegEx expert. This is Chinese for me.

The search string provided is not understood by regex101.com

If you are an expert, your help would be greatly appreciated.

 

@David

What do you mean exactly by scripting language ?

How this could help writing a "formula" to convert such strings ?

 

Share this post


Link to post

If the strings are all in one file then notepad++ and a macro or two should work.

Share this post


Link to post

Sorry, I can't write it for you - don't avoid learning REs. They're not so scary as they look and you likely need them at some moment.

Share this post


Link to post
55 minutes ago, Bob Baudewyns said:

@David

What do you mean exactly by scripting language ?

How this could help writing a "formula" to convert such strings ?

I mean if you have a bunch of files that need to transformed once, then writing delphi code isn't the most efficient. You'd just use a scripting language to make the change and throw the code away. 

 

But if you have a Delphi product that needs to repeatedly perform this transformation  then that's not going to be useful. 

Share this post


Link to post
18 hours ago, Bob Baudewyns said:

Oh, I see.

Unfortunately not.

All of this is inside Delphi code.

so copy it from the Delphi code, paste it into a new panel in Notepad++, do the work, then copy it and paste it over the old code in Delphi. I do it all the time to isolate chunks of code I want to change because its easier than having to keep restricting the bounds on what's replaced inside of the IDE.

 

Or ... are you saying this needs to be done at run-time?

Edited by David Schwartz

Share this post


Link to post

The string conversion is supposed to be done by the end user of the application inside the GUI

Not from the IDE by the developer.

So, RegEx, running "behind the scene"  looks like the best option here.

Share this post


Link to post

Thank you for your feedback.
I managed to solve my RegEx problems quite some time ago with the help of another member (Fr0sT.Brutal)

But this is indeed a very interesting approach.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×