Recent

Author Topic: Regex for changing component names.  (Read 978 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Regex for changing component names.
« on: September 01, 2024, 09:22:33 pm »
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.
Lazarus 3.0/FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #1 on: September 02, 2024, 07:27:57 am »
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.
« Last Edit: September 02, 2024, 08:37:27 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #2 on: September 02, 2024, 08:03:39 am »
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.
« Last Edit: September 02, 2024, 08:32:31 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Zvoni

  • Hero Member
  • *****
  • Posts: 2724
Re: Regex for changing component names.
« Reply #3 on: September 02, 2024, 08:34:47 am »
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.
I would probably go for all lower case, since our Pascal is case-insensitive regarding variable- or type-names ("TEdit1 = tedit1")
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #4 on: September 02, 2024, 08:41:25 am »
I 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
« Last Edit: September 02, 2024, 09:10:11 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

dseligo

  • Hero Member
  • *****
  • Posts: 1395
Re: Regex for changing component names.
« Reply #5 on: September 02, 2024, 10:06:24 am »
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.

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

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #6 on: September 02, 2024, 11:11:44 am »
Are you sure? Delphi does that correctly.
Code: Pascal  [Select][+][-]
  1. object Mainform: TMainform <-- refactored
  2.   Left = 0
  3.   Top = 0
  4.   Caption = 'Mainform' <-- refactored
  5.   ClientHeight = 441
  6.   ClientWidth = 624
  7.   Color = clBtnFace
  8.   Font.Charset = DEFAULT_CHARSET
  9.   Font.Color = clWindowText
  10.   Font.Height = -12
  11.   Font.Name = 'Segoe UI'
  12.   Font.Style = []
  13.   TextHeight = 15
  14. end
I expected Lazarus to do the same: if you refactor a classname, Delphi also updates the dfm accordingly.
« Last Edit: September 02, 2024, 11:13:49 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

dseligo

  • Hero Member
  • *****
  • Posts: 1395
Re: Regex for changing component names.
« Reply #7 on: September 02, 2024, 11:13:39 am »
Are you sure? Delphi does that correctly.

I don't use Delphi. I just tried with trunk and 3.4.

Thaddy

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #8 on: September 02, 2024, 11:15:21 am »
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)
« Last Edit: September 02, 2024, 11:22:34 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

dseligo

  • Hero Member
  • *****
  • Posts: 1395
Re: Regex for changing component names.
« Reply #9 on: September 02, 2024, 11:21:39 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 16031
  • Censorship about opinions does not belong here.
Re: Regex for changing component names.
« Reply #10 on: September 02, 2024, 11:23:05 am »
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.
« Last Edit: September 02, 2024, 11:28:47 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Re: Regex for changing component names.
« Reply #11 on: September 02, 2024, 12:02:54 pm »
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.

<snip>

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.
Lazarus 3.0/FPC 3.2.2

n7800

  • Full Member
  • ***
  • Posts: 170
Re: Regex for changing component names.
« Reply #12 on: September 08, 2024, 03:05:46 am »
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.

They are not closed, but all are still open. Although I have now suggested closing one, as it is clearly a duplicate.

 

TinyPortal © 2005-2018