EcoStruxure SEAL Forum
This forum is for engineers working EcoStruxure Building Operation, wanting to leverage the SEAL application to improve the efficiency in the engineering process.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2024-12-10 05:09 AM . Last Modified: 2024-12-10 05:57 AM
The search and replace script is a powerful tool and with this update we’re leveling it up even more. However, the tool is only as powerful as its user and therefore this is your chance to level up too!
In Search and Replace version 3.4.0 we're introducing Regular Expressions (Regex) as an option when searching. In short Regex is a pattern matching standard that is widely agreed upon and allows you to create powerful patterns that can match text more precisely OR more generally than simple 1:1 matching could ever do.
Writers note: This might seem daunting at first, however, I assure you that any time you spend learning Regex will be paid back ten-fold if your work involves any kind of data manipulation.
What does Regex do?
Regex allows you to write patterns instead of words when trying to find whatever it is that you want to replace. For example, trying to replace something short like “P1” => “P2” can be a problem since “P1” will appear in more places than you might expect. Imagine you have 2 folders, what would happen if you replaced “P1” with “P2”:
P1 => P2
P100 => P200
P100 also got renamed despite that not being my intention. Let’s see how Regex can help us in this situation.
As mentioned, Regex allows you to write patterns using tokens that match multiple characters according to a set of rules. For example, to solve the issue above we could use the “\b” token to denote that we only want to match “P1” when it is alone. We could write our pattern as: “\bP1\b” and that would result in the following:
P1 => P2
P100 => No Match
Why? Because now we told the Regex engine to search for any “word boundary(\b)” followed by “P1” and then another “word boundary(\b)”. P100 stops matching since the zero that is after “P1” breaks the rules.
Exactly what a “word boundary” is would not be the most logical place to start learning Regex, but it is a good example of a simple and applicable case that might come in handy a lot.
Resources
https://regex101.com/ – The number 1 tool to both learn and test Regex patterns. Use this while writing your patterns to get live feedback on what matches what and explanations of all available tokens.
https://www.youtube.com/watch?v=jCAyQ7C71m4 – This 8-minute video I deemed to be the “best” after searching YouTube for “regex basic usage”.
Ask a chatbot – By asking a chatbot to make a regex to match some provided text you will probably get a pattern that is 90% there. Just make sure to read it through and clean up any unnecessary backslashes etc...
And finally, this community itself! If you need a regex pattern to save you 4 hours of manual labour but don’t have the time or interest to understand it yourself, post a VERY DETAILED question in the forum and see if another Regex wizard can help you out in designing your pattern. Remember you must provide both what you want to match as well as what you don’t want to match.
More tokens
Token |
Explanation |
\d |
Any digit. Ex. 1,2,3,4,5,6,7,8,9,0 |
\D |
Any non-digit. The reverse of the above token |
\s |
Any whitespace character. Ex. Space, tab, return (newline) |
\S |
Any non-whitespace character. The reverse of above token |
\w |
Any word character. Ex. 1,2,3,a,b,c,_ (underscore) |
\W |
Any non-word character. The reverse of above token |
? |
Zero or one of the previous character. Ex. ab? Matches “a” and “ab” but not “b” |
* |
Zero or more of the previous character. Ex. ab* matches “a”, “ab”, “abbb” but not “aab” |
+ |
One or more of the previous character. Ex. ab+ matches “ab”, “abb” but not “a” or “b” |
. |
Any character. Ex. a. Matches “a” followed by a single character like “ab”, “ac”, “a1” |
\b |
Word boundary matches the beginning or end of words. Best to test yourself. |
And many more, please see https://regex101.com/ for more tokens in their “Quick reference” in the bottom right corner. Also try them out in their editor.
That’s it for now, and as always, backup your data before performing any Search and Replace action!
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2024-12-10 04:14 PM
Nice and thorough post @Jim.T ! I've used RegEx testing/builder tools in the past (http://www.regex-match-tracer.com being my go-to), but for sure with AI and what other sites offers these days, I myself have to revisit my approach 🙂
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2024-12-12 03:07 AM
A typical example where a regex in Search and Replace solves some tricky cases is as follows:
A heating application following Swedish standards contains the following components/identifiers:
You want to use Search and Replace to change the pump identifier from P1 to P2 without altering any other objects.
According to the Swedish naming convention, the names of IOs/variables/alarms for a pump named P1 would look like this and are named similarly in TGML and Functionblock programs, and you want the Search and Replace to take care of all of these:
To achieve this, you should be able to use the following regex pattern in the Search and Replace script:
(?<![a-zA-Z0-9åäöÅÄÖ])P1(?![0-9])
(?<![a-zA-Z0-9åäöÅÄÖ])
: This is a negative lookbehind assertion. It ensures that the position before P1
is not preceded by any of the characters in the set [a-zA-Z0-9åäöÅÄÖ]
. This set includes:
a-z
, A-Z
)0-9
)å
, ä
, ö
, Å
, Ä
, Ö
)P1
: This is the literal string P1
that the pattern is trying to match.
(?![0-9])
: This is a negative lookahead assertion. It ensures that the position after P1
is not followed by any digit (0-9
).
In summary, this regex pattern matches the string P1
only if it is not preceded by any alphanumeric character or Swedish character (å
, ä
, ö
, Å
, Ä
, Ö
) and is not followed by any digit.
Use the pattern like this in the Search & Replace script:
(Ensure you select Regular Expression as the operator)
Link copied. Please paste this link to share this article on your social media post.
Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.