In PowerShell, I’m looking to convert a SharePoint Location string (john_smith_domain_com) to the proper addressing of john.Smith@domain.com.
I’m not sure which is the best way to go about doing this.
I know $var.Replace("_",".") would work to replace all the "_"s with "."s, but that doesn’t help the "@" going between the name and the domain.
Unfortunately, the domain isn’t always the same, or I’d be able to just .replace it easily.
>Solution :
You can combine two -replace operations (this assumes a two-component domain name, such as domain.com):
# -> 'john.smith@domain.com'
'john_smith_domain_com' -replace '_(?![^_]+_[^_]+$)', '.' -replace '_', '@'
-
Regex
_(?![^_]+_[^_]+$)matches all_chars. except the second-to-last one.- For an explanation of the regex and the ability to interact with it, see this regex101.com page.
-
After all these have been replaced with
., only the second-to-last one is left, which can then be replaced with@