Regex for changing component names. lazarus / 9-Editor ---------------------------------------------------------------------- vfclists · 2024-09-01 19:22 When you make a copy of group of controls they usually have a number affixed to them if there is no conflict with a control with the same name. I need a regex that replaces all component names ending with a number with a string that I guarantee to make them unique, inside the LFM. Most component entries in the LFMs are of the form: object ComponentName{numeric suffix}: T{ComponentName} So the idea is to search and replace {single-digit}: T => {newuniquetextsuffix}: T The T can be extended with component name suffixes to reduce the chance of errors. Sorry to say this but regex is not one of my strongest points, something I've been panned for in other forums, but I need to start creating a library of them. ---------------------------------------------------------------------- Thaddy · 2024-09-02 05:27 Better do not do that. editing the lfm by hand has a 99% chance you will loose sync with your code. Use the code refacoring options in the Lazarus IDE. Ctrl-Shift-E. The dialog has the option to rename all references in your project, not just the form and keeps everything sync'd. A regular extression looks something like this: '^\bT[A-Z][A-Za-z0-9]*[0-9]\b$' explanation: \bT word boundary starts with capital T [A-Z] followed by a second captital [A-Za-z0-9]* followed by arbitrary ansi [0-9]\b ends with digits as word boundary. Note it is probably more complex than this to get it right, although this should cover the common case, expect false positives or negatives. ---------------------------------------------------------------------- Thaddy · 2024-09-02 06:03 Note I editted the regexpr to cover more cases into '^\bT[A-Z][A-Za-z0-9]*[0-9]\b$' Starts with capital T, followed by another capital, followed by arbitrary ansi and ends on a number. This is how the IDE initially names the components This covers things like TButton1023 or TBu5tTon10 too. If you need to cover for Tbutton, lower case b, too then you can remove the first[A-Z] By now tested on regex101.com and they work as I expected. I tested this one too that goes like this: '^\bT[A-Za-z0-9_]*[0-9]\b$' Starts with captital T followed by arbitrary ansi, including underscores and ends on a number. ---------------------------------------------------------------------- Zvoni · 2024-09-02 06:34 Quote from: Thaddy on September 02, 2024, 08:03:39 amNote I editted the regexpr to cover more cases into '^\bT[A-Z][A-Za-z0-9]*[0-9]\b$' Starts with capital T, followed by another capital, followed by arbitrary ansi and ends on a number. This is how the IDE initially names the components This covers things like TButton1023 or TBu5tTon10 too. If you need to cover for Tbutton, lower case b, too then you can remove the first[A-Z] By now tested on regex101.com and they work as I expected. I tested this one too that goes like this: '^\bT[A-Za-z0-9_]*[0-9]\b$' Starts with captital T followed by arbitrary ansi, including underscores and ends on a number. I would probably go for all lower case, since our Pascal is case- insensitive regarding variable- or type-names ("TEdit1 = tedit1") ---------------------------------------------------------------------- Thaddy · 2024-09-02 06:41 Quote from: Zvoni on September 02, 2024, 08:34:47 amI would probably go for all lower case, since our Pascal is case-insensitive regarding variable- or type-names ("TEdit1 = tedit1") The case at hand is how the IDE generates the control names and that follows the rules as expressed in '^\bT[A-Z][A-Za-z0-9]*\d\b$' or alternatively '^\bT[A-Z][A-Za-z]*[0-9]*\b$' - capital T - arbitray second captital - arbitrary ansi - ending on a number. You can't ignore case for this task because you must follow the rules that the IDE applies when it generates component names. That said I would use refactoring with ctrl-shift-e ---------------------------------------------------------------------- dseligo · 2024-09-02 08:06 Quote from: Thaddy on September 02, 2024, 07:27:57 amUse the code refacoring options in the Lazarus IDE. Ctrl-Shift-E. The dialog has the option to rename all references in your project, not just the form and keeps everything sync'd. Ctrl+Shift+E doesn't change names in .lfm, only in source files. Names of components on the form must be changed through object inspector. ---------------------------------------------------------------------- Thaddy · 2024-09-02 09:11 Are you sure? Delphi does that correctly.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i object Mainform: TMainform <-- refactored  Left = 0  Top = 0  Caption = 'Mainform' <-- refactored  ClientHeight = 441  ClientWidth = 624  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -12  Font.Name = 'Segoe UI'  Font.Style = []  TextHeight = 15endI expected Lazarus to do the same: if you refactor a classname, Delphi also updates the dfm accordingly. ---------------------------------------------------------------------- dseligo · 2024-09-02 09:13 Quote from: Thaddy on September 02, 2024, 11:11:44 amAre you sure? Delphi does that correctly. I don't use Delphi. I just tried with trunk and 3.4. ---------------------------------------------------------------------- Thaddy · 2024-09-02 09:15 I might consider that a bug. ctrl-shift-e should also update the lfm file. Should not be too hard to fix. After all there already is an lfm parser. At least now I see the point why the regex is needed or wanted. It should not be needed, the IDE should sychronize the code AND the lfm. Delphi has that for 20 years (D2005) ---------------------------------------------------------------------- dseligo · 2024-09-02 09:21 Quote from: Thaddy on September 02, 2024, 11:15:21 amI might consider that a bug. ctrl-shift-e should also update the lfm file. Should not be too hard to fix. After all there already is an lfm parser. Already reported: https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/29423 https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/13843 https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/26563 ---------------------------------------------------------------------- Thaddy · 2024-09-02 09:23 Tnx. I really do not understand why they are closed without further action. The oldest one is 15 years ago. Will try to re-open them. ---------------------------------------------------------------------- vfclists · 2024-09-02 10:02 Quote from: Thaddy on September 02, 2024, 07:27:57 amBetter do not do that. editing the lfm by hand has a 99% chance you will loose sync with your code. Use the code refacoring options in the Lazarus IDE. Ctrl-Shift-E. The dialog has the option to rename all references in your project, not just the form and keeps everything sync'd. It is intended to do bulk renaming of complex component hierachies, so the events attached them don't  matter. The idea is to copy the components to a mostly empty different form where the copy is unlikely to affect the component names, then they are changed to a new unique suffix then copied back to the original form. It also helps to track version control as the changes are all done in a single batch of nested components. These are mostly visual components. ---------------------------------------------------------------------- n7800 · 2024-09-08 01:05 Quote from: Thaddy on September 02, 2024, 11:23:05 amTnx. I really do not understand why they are closed without further action. The oldest one is 15 years ago. Will try to re-open them. They are not closed, but all are still open. Although I have now suggested closing one, as it is clearly a duplicate.