Bob Baudewyns 3 Posted January 11, 2022 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
Fr0sT.Brutal 900 Posted January 11, 2022 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
David Heffernan 2345 Posted January 11, 2022 If this is a one time transformation then it's easier with a scripting language Share this post Link to post
Bob Baudewyns 3 Posted January 11, 2022 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
Bernard 18 Posted January 11, 2022 If the strings are all in one file then notepad++ and a macro or two should work. Share this post Link to post
Bob Baudewyns 3 Posted January 11, 2022 Oh, I see. Unfortunately not. All of this is inside Delphi code. Share this post Link to post
Fr0sT.Brutal 900 Posted January 11, 2022 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
David Heffernan 2345 Posted January 11, 2022 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
timfrost 78 Posted January 11, 2022 Regex Buddy (https://www.regexbuddy.com) is a useful resource for learning and testing Regex. There is a money-back guarantee in case it does not float your boat. Share this post Link to post
Rollo62 536 Posted January 11, 2022 18 minutes ago, timfrost said: Regex Buddy (https://www.regexbuddy.com) is a useful resource for learning and testing Regex. There is a money-back guarantee in case it does not float your boat. You can also find a nice online tool Share this post Link to post
David Schwartz 426 Posted January 12, 2022 (edited) 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 January 12, 2022 by David Schwartz Share this post Link to post
Bob Baudewyns 3 Posted January 12, 2022 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
Berocoder 14 Posted May 2, 2022 https://ihateregex.io/ Is good regex resource Share this post Link to post
Bob Baudewyns 3 Posted May 3, 2022 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