'PowerShell: Remove text from $result variable
I need to remove the (UTC-07:00) from $result. How can this be done?
$Result = '(UTC-07:00) Mountain Time (US & Canada)'
Solution 1:[1]
Use the -replace regex operator:
$Result = '(UTC-07:00) Mountain Time (US & Canada)'
$TimeZoneName = $Result -replace '^\(UTC[+-][01][0-9]:[0134][05]\)\s*'
The regular expression pattern used describes:
^ # start of string
\( # a literal `(`
UTC # the string `UTC`
[+-] # a `+` or a `-`
[01][0-9]:[0134][05] # a HH:mm timestamp
\) # a literal `)`
\s* # 0 or more whitespace characters
The expression used for the timestamp ([01][0-9]:[0134][05]) might look a bit strange, but time zone offsets are always in 15 minute increments so we only need to match 00, 15, 30 and 45 as the mm part.
As a result, the string value stored in $TimeZoneName is now "Mountain Time (US & Canada)"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mathias R. Jessen |
