# Extract substrings from string (parsing expression to get all tag references within)

**URL:** <https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906>\
**Category:** Ignition\
**Created:** [March 6, 2024, 3:50pm UTC](https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906 "2024-03-06T15:50:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Nicks](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@David\_Nicks](https://forum.inductiveautomation.com/u/David_Nicks)\
**Post date:** [March 6, 2024, 3:50pm UTC](https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906/1 "2024-03-06T15:50:58Z")

</div>

I'm using system.tag.getConfiguration() to get the expression out of an expression tag. From this, I want to extract a list of all the tags referenced in the expression. For example, if the expression is

`{[.]../FolderA/Tag1}||{[.]../Tag2}||{[.]../FolderB/FolderC/Tag3}`

I would like to return

`{"[.]../FolderA/Tag1","[.]../Tag2","[.]../FolderB/FolderC/Tag3"}`

I was hoping to find a script function that would search a string using regex to find a substring match, but I'm not seeing anything comparable to the IndexOf expression function.

Any recommendations on how to do this?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 6, 2024, 4:00pm UTC](https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906/2 "2024-03-06T16:00:50Z")

</div>

Searching "regex to find substrings enclosed in special characters" on google, the first result is this:

> <https://stackoverflow.com/questions/4786556/regex-to-find-substring-between-special-characters>

Replace their characters with yours and you should be okay.

edit: meh. This regex is a bit too specific to the user's case. Should have read BEFORE replying.  
I'll try to make it work

edit2:  
maybe this ?  
`re.findall(r"\{[^}]*\}", s)`

- `\{` matches the opening `{`
- `[^}]*` matches everything until the closing `}`
- `\}` matches the closing '}'

if you want to make sure there's a provider: `re.findall(r"\{\[[^]]*\][^}]*\}", s)`

edit3:  
actually... something like this ?  
`re.findall(r"(?<={)[^}]*", s)`

- `(?<={)` selects things AFTER the opening `{`
- `[^}]*` selects everything up to the closing `}`

This way the `{}` are not included in the results

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 6, 2024, 4:45pm UTC](https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906/3 "2024-03-06T16:45:47Z")

</div>

I group the look-aheads and look-behinds. To me, anyway, the pattern is more readable.

```python
import re
        
regex = r"(?<={)(.*?)(?=})"

test_str = "{[.]../FolderA/Tag1}||{[.]../Tag2}||{[.]../FolderB/FolderC/Tag3}"

matches = re.findall(regex, test_str, re.MULTILINE)

print matches

```

Output:

```plaintext
['[.]../FolderA/Tag1', '[.]../Tag2', '[.]../FolderB/FolderC/Tag3']

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 6, 2024, 4:47pm UTC](https://forum.inductiveautomation.com/t/extract-substrings-from-string-parsing-expression-to-get-all-tag-references-within/86906/4 "2024-03-06T16:47:52Z")

</div>

> [@David\_Nicks](#):
>
> {[.]../FolderA/Tag1}||{[.]../Tag2}||{[.]../FolderB/FolderC/Tag3}

Just use Java's String.split()

I find that it's easier if you include the delimiters, that way you can eliminate any expressions that might be between tags.

I assume from your expected output that you were wanting a set, which would eliminate duplicate tag entries. A list could just as easily be used.

There is a `java.util.StringTokenizer` which will do this same thing, however, that has been deprecated. The recommended method is now `String.split()`

```python
from java.lang import String

test = "{[.]../FolderA/Tag1}||{[.]../Tag2}||{[.]../FolderB/FolderC/Tag3}"

tokens = String(test).split("((?=\{|\})|(?<=\{|\}))")

tags = set()
enumToken = enumerate(tokens)

for cursor,token in enumToken:
	if token == "{":
		#start of tag path, consume the token
		cursor,token = next(enumToken)
		tags.add(token)
		
		#consume the ending brace
		cursor,token = next(enumToken)

print tags

```

output:

```auto
>>> 
set([u'[.]../Tag2', u'[.]../FolderA/Tag1', u'[.]../FolderB/FolderC/Tag3'])
>>> 

```
