Forum > Editor

Regex for changing component names.

(1/3) > >>

vfclists:
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:
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:
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:

--- Quote from: Thaddy 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.

--- End quote ---
I would probably go for all lower case, since our Pascal is case-insensitive regarding variable- or type-names ("TEdit1 = tedit1")

Thaddy:

--- Quote from: Zvoni on September 02, 2024, 08:34:47 am ---I would probably go for all lower case, since our Pascal is case-insensitive regarding variable- or type-names ("TEdit1 = tedit1")

--- End quote ---
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

Navigation

[0] Message Index

[#] Next page

Go to full version