The power of regular expressions
In some cases, migrations require smart ways to find and replace specific string-values supporting one or more conditions at the same time. Regular expressions can be super useful to accomplish specific matches to initiate replacements. This post contains a real world example illustrating the power of regex.
Setting the stage
For the Dutch language, the default teams channel is called ‘Algemeen’. However, the corresponding channel folder in SharePoint Online is called ‘General’. So during a migration for Dutch localized Teams workspaces, the default channel name needs to be replaced in order to migrate files into the channel folder.
The logic is pretty straight forward:
$channelName -replace 'Algemeen', 'General'
And even when adding a subfolder path in the inputlist, e.g. ‘Algemeen/Subfolder’, the logic stays simple:
$channelName.Split('/')[0] -replace 'Algemeen', 'General'
But, what if $channelName contains the word ‘Algemeen’, e.g. ‘Algemeen HR’, ‘HR Algemeen’, ‘HR-Algemeen’ or something similar. Then a simple -replace will not work. The replace-action needs to meet the following conditions:
- A match on the whole word ‘Algemeen’ without any characters before or after. Just the channel name.
- The whole word ‘Algemeen’ needs to be at the beginning of the string, directly followed by the forward slash.
If there is no match, then the word ‘Algemeen’ should not be replaced. Like in ‘HR Algemeen’.
Regex to the rescue
That’s where regular expressions can be of service. A regular expression (regex) is a sequence of characters that specifies a match pattern in text. Like the conditions in this example.
So, the code is adjusted to the following:
$channelName -replace '^(Algemeen)(?=$|\/)', 'General'
Let’s dissect this:
- The ‘^’ -character is used to match specific characters at the beginning of a string. In this case, the word ‘Algemeen’.
- The last part, `(?=$|\/)`, tries to match the forward slash, but in an optional way.
With this code, both conditions can be met and tested against the following use cases:
$channelName = 'Algemeen' #Replaced by 'General'
$channelName = 'Algemeen Afdeling/Subfolder' #No replace
$channelName = 'Algemeen/Subfolder/Algemeen' #Only replaces the first occurance of 'Algemeen'
$channelName = 'Algemeen_HR/Subfolder/Algemeen' #No replace
$channelName = 'HQ-Algemeen/Subfolder/Algemeen' #No replace
$channelName = 'HQ-Algemeen-HR/Subfolder/Algemeen' #No replace
$channelName = 'Algemeen HR/Subfolder/Algemeen' #No replace
$channelName = 'HR Algemeen/Subfolder/Algemeen' #No replace
Final thoughts
Regex is not easy! And some people even try to avoid it. To really dive into it and write it from your own memory, takes a lot of time and frustration.
So, AI-tools like ChatGPT or Microsoft Copilot are your friends (next to PowerShell, of course) to be more productive.
By clearly explaining the conditions that should be met, the prompt is able to return a regex pattern that can be tested against the conditions and use cases.
It’s definitely not a flawless process, as the LLM needs to get the right prompt and potentially refinement prompts to get it accurate. But, it is a lot quicker than browsing the web to find something that might fit.