Bulk rename of rear-ports duplicates replace pattern #7778

Closed
opened 2025-12-29 20:28:04 +01:00 by adam · 8 comments
Owner

Originally created by @phurrelmann on GitHub (Mar 21, 2023).

Originally assigned to: @decoupca on GitHub.

NetBox version

v.3.4.3

Python version

3.9

Steps to Reproduce

Select a range of rear-ports and try to bulk rename them.
The static part of the replace pattern is being duplicated.

Steps to reproduce:

  1. Create a device multiple rear-ports.
  2. Select multiple of rear-ports and hit rename to open the bulk rename template
  3. Enter the regex "(.*)" into the find field and enter "Dummy\1" into the replace field
  4. Hit Preview

Expected Behavior

All rear-ports are renamed and prepended with the prefix "Dummy"

Observed Behavior

All rear-ports are renamed and prepended and appended with "Dummy".

Screenshot from our local installation (v.3.4.3):
image

Screenshot from netbox demo (v3.4.6):
image

Originally created by @phurrelmann on GitHub (Mar 21, 2023). Originally assigned to: @decoupca on GitHub. ### NetBox version v.3.4.3 ### Python version 3.9 ### Steps to Reproduce Select a range of rear-ports and try to bulk rename them. The static part of the replace pattern is being duplicated. Steps to reproduce: 1. Create a device multiple rear-ports. 2. Select multiple of rear-ports and hit rename to open the bulk rename template 3. Enter the regex "(.*)" into the find field and enter "Dummy\1" into the replace field 4. Hit Preview ### Expected Behavior All rear-ports are renamed and prepended with the prefix "Dummy" ### Observed Behavior All rear-ports are renamed and prepended **and** appended with "Dummy". Screenshot from our local installation (v.3.4.3): ![image](https://user-images.githubusercontent.com/10113716/226578103-8ec730a9-1ea7-4cf4-9ed1-ac8326ee8660.png) Screenshot from netbox demo (v3.4.6): ![image](https://user-images.githubusercontent.com/10113716/226577889-4854be41-5a5c-472c-9501-0e9fee4769fb.png)
adam added the type: bug label 2025-12-29 20:28:04 +01:00
adam closed this issue 2025-12-29 20:28:05 +01:00
Author
Owner

@phurrelmann commented on GitHub (Mar 21, 2023):

The same behavior is seen on bulk-rename of interfaces and front-ports.
A workaround is to make the regex match the start, too by using "^(.*)".

@phurrelmann commented on GitHub (Mar 21, 2023): The same behavior is seen on bulk-rename of interfaces and front-ports. A workaround is to make the regex match the start, too by using "^(.*)".
Author
Owner

@decoupca commented on GitHub (Mar 21, 2023):

I'll take this one

@decoupca commented on GitHub (Mar 21, 2023): I'll take this one
Author
Owner

@kkthxbye-code commented on GitHub (Mar 21, 2023):

Maybe I'm misunderstanding, but how is this a bug?

The * token means zero or more, which matches empty string and the string. The + token means one or more:

image
@kkthxbye-code commented on GitHub (Mar 21, 2023): Maybe I'm misunderstanding, but how is this a bug? The * token means zero or more, which matches empty string and the string. The + token means one or more: <img width="1172" alt="image" src="https://user-images.githubusercontent.com/400797/226665910-f22d7cb8-7cfc-43a8-b3cd-76222031b87a.png">
Author
Owner

@jeremystretch commented on GitHub (Mar 21, 2023):

I haven't tried reproducing this myself, but the screenshot shows the string Dummy being added to both the beginning and end of the new name. Not sure what would cause that.

@jeremystretch commented on GitHub (Mar 21, 2023): I haven't tried reproducing this myself, but the screenshot shows the string `Dummy` being added to both the beginning and end of the new name. Not sure what would cause that.
Author
Owner

@phurrelmann commented on GitHub (Mar 21, 2023):

Maybe I'm misunderstanding, but how is this a bug?

The * token means zero or more, which matches empty string and the string. The + token means one or more:

image

Understood. There are several ways to match. But not the matching seems to be the problem, but the replace. Both (your and my example) match the whole source as a group and that retained in the replace by "\1". The question is why is "Dummy" prepended and appended in my example, but not in others.

Working correctly:
match: "^(.*)" and "(.+)"

not working:
match: "(.*)"

@phurrelmann commented on GitHub (Mar 21, 2023): > Maybe I'm misunderstanding, but how is this a bug? > > The * token means zero or more, which matches empty string and the string. The + token means one or more: > > <img alt="image" width="1172" src="https://user-images.githubusercontent.com/400797/226665910-f22d7cb8-7cfc-43a8-b3cd-76222031b87a.png"> Understood. There are several ways to match. But not the matching seems to be the problem, but the replace. Both (your and my example) match the whole source as a group and that retained in the replace by "\1". The question is why is "Dummy" prepended _and_ appended in my example, but not in others. Working correctly: match: "^(.*)" and "(.+)" not working: match: "(.*)"
Author
Owner

@decoupca commented on GitHub (Mar 21, 2023):

Maybe I'm misunderstanding, but how is this a bug?

The * token means zero or more, which matches empty string and the string. The + token means one or more:

image

Correct, this is not a bug with either NetBox or re.sub. Either ^(.*) or (.+) works.

It matches empty string first, appending Dummy, then matches full string, appending Dummy again.

A worse workaround would be to add count=1 to the re.sub call, but that would limit all other regex searches to replacing one instance.

@decoupca commented on GitHub (Mar 21, 2023): > Maybe I'm misunderstanding, but how is this a bug? > > The * token means zero or more, which matches empty string and the string. The + token means one or more: > > <img alt="image" width="1172" src="https://user-images.githubusercontent.com/400797/226665910-f22d7cb8-7cfc-43a8-b3cd-76222031b87a.png"> Correct, this is not a bug with either NetBox or `re.sub`. Either `^(.*)` or `(.+)` works. It matches empty string first, appending Dummy, then matches full string, appending Dummy again. A worse workaround would be to add `count=1` to the `re.sub` call, but that would limit all other regex searches to replacing one instance.
Author
Owner

@kkthxbye-code commented on GitHub (Mar 21, 2023):

Understood. There are several ways to match. But not the matching seems to be the problem, but the replace. Both (your and my example) match the whole source as a group and that retained in the replace by "\1". The question is why is "Dummy" prepended and appended in my example, but not in others.

Working correctly: match: "^(.*)" and "(.+)"

not working: match: "(.*)"

I'm saying this is how regex engines work to my knowledge. Here's regexr.com:

Screenshot 2023-03-21 at 17 06 47

Do you have an example where this causes an issue when using the right regex token?

The question is why is "Dummy" prepended and appended in my example, but not in others.

I thought I explained that. Here's a stackoverflow that says the same thing:

https://stackoverflow.com/questions/27081446/why-does-my-regex-replace-string-contain-the-replacement-value-twice

I might be wrong though, but I think we need an example where this causes an issue. Your issue is obviously easily solved by using the correct token.

@kkthxbye-code commented on GitHub (Mar 21, 2023): > Understood. There are several ways to match. But not the matching seems to be the problem, but the replace. Both (your and my example) match the whole source as a group and that retained in the replace by "\1". The question is why is "Dummy" prepended _and_ appended in my example, but not in others. > > Working correctly: match: "^(.*)" and "(.+)" > > not working: match: "(.*)" I'm saying this is how regex engines work to my knowledge. Here's regexr.com: <img width="562" alt="Screenshot 2023-03-21 at 17 06 47" src="https://user-images.githubusercontent.com/400797/226669330-85713eea-9f0e-4cc9-9d9d-ed2284721c83.png"> Do you have an example where this causes an issue when using the right regex token? > The question is why is "Dummy" prepended and appended in my example, but not in others. I thought I explained that. Here's a stackoverflow that says the same thing: https://stackoverflow.com/questions/27081446/why-does-my-regex-replace-string-contain-the-replacement-value-twice I might be wrong though, but I think we need an example where this causes an issue. Your issue is obviously easily solved by using the correct token.
Author
Owner

@phurrelmann commented on GitHub (Mar 21, 2023):

Ok, so I was mistakenly expecting it to work like re.sub with "count=1". if the defaults are fine for everyone, then this is no bug and the issue can be closed.

Thanks for the fast responses from all and sorry for the noise.

@phurrelmann commented on GitHub (Mar 21, 2023): Ok, so I was mistakenly expecting it to work like re.sub with "count=1". if the defaults are fine for everyone, then this is no bug and the issue can be closed. Thanks for the fast responses from all and sorry for the noise.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7778