Home > Mobile >  PowerShell: Remove text from $result variable
PowerShell: Remove text from $result variable

Time:04-14

I need to remove the (UTC-07:00) from $result. How can this be done?

$Result = '(UTC-07:00) Mountain Time (US & Canada)'

CodePudding user response:

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)"

  • Related