# General question, escape square brackets in string while using boundaries

**URL:** <https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978>\
**Category:** General Discussion\
**Tags:** ignition81, perspective, scripting\
**Created:** [May 6, 2024, 5:30pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978 "2024-05-06T17:30:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [May 6, 2024, 5:30pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978/1 "2024-05-06T17:30:52Z")

</div>

I'm struggling to find a solution, I'm trying to make a simple app in Perspective that can take FactoryTrash ME window exports and do a search and replace. I have a lookup table that is the current address and what the new address should be.

Here is a short example:

```python
import re

oldAddress = "::[PLC]N22:21"
newAddress = "[PLC]N22[21]"
data = '<connection name="Value" expression="{::[PLC]N22:211} * .1"/>'
data = data.replace(oldAddress, newAddress)
print data

```

Result:

```python
<connection name="Value" expression="{[PLC]N22[21]1} * .1"/>

```

The issue is it should be:

```python
<connection name="Value" expression="{[PLC]N22[21]} * .1"/>

```

When I want to match whole word boundaries I will use this:

```python
data = re.sub(r"\b%s\b" % re.escape(oldAddress), newAddress, data)

```

However, this doesn't work with square brackets in the string.

How can I escape square brackets and still search with boundaries?

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [May 6, 2024, 6:15pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978/2 "2024-05-06T18:15:31Z")

</div>

To have regex see square brackets, you have to escape them with `\`.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/b/0bc59a3e8600582e763ce2ae75cd91d726577b06.png)

However, help me understand. Your oldAddress ends with 21, not 211, so it would not replace the last 1 in the data string because its not part of the searched string. Don't you want your oldAddress to be _::[PLC]N22:211_?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [May 6, 2024, 6:21pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978/3 "2024-05-06T18:21:36Z")

</div>

I tried escaping with backslashes and I couldn't get it to work, but perhaps I was overlooking something.

In my example, nothing should have been replaced, `{::[PLC]N22:211}` should have remained as-is. My point was using `replace` would still replace the text, which is why I wanted to use boundaries.

I'll be honest, this app is really simple and it's just something I want to use for conversion projects. So, not important, IMO, to be best practice, so for right now I just added back in the curly brackets, then do the replace. I should have mentioned this is replacing direct tags which are always wrapped in curly brackets and will include a shortcut (the stuff with square brackets).

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 6, 2024, 6:25pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978/4 "2024-05-06T18:25:18Z")

</div>

I found some of the online regex generators useful. e.g.

> **[RegExr: Learn, Build, & Test RegEx](https://regexr.com/)**
>
> RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [May 6, 2024, 6:35pm UTC](https://forum.inductiveautomation.com/t/general-question-escape-square-brackets-in-string-while-using-boundaries/89978/5 "2024-05-06T18:35:17Z")

</div>

You can end your regex expression with `(?=})` to specify that is should be followed by a curly brace but not included. This is a [positive lookahead](https://www.regular-expressions.info/lookaround.html).

Not matching:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/0/80741db7de2d67b235134956e4625d224cec2907.png)

Matching:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/4/e405124b7e3f476b608e80c7aae16c3c25c0cc13.png)
