Home > OS >  Powershell to match the pattern at end of string and print the first part of the string
Powershell to match the pattern at end of string and print the first part of the string

Time:11-04

I have a string "dep-value-sync-excess-v12" or "dep-value-sync-excess-v100" ,

The pattern to be matched is anything that ends with -v and print "dep-value-sync-excess"

I am able to pattern match but not print the first part of string

Any help would be appreciated

CodePudding user response:

"The pattern to be matched is anything that ends with ... print the first part of string"
Means: Remove (replace with nothing) the last part of the string

Thus, using the -Replace operator:

$String = "dep-value-sync-excess-v12", "dep-value-sync-excess-v100"
$String -Replace '-v\d $'
dep-value-sync-excess
dep-value-sync-excess

Regular Expression explanation:

  • -v literal match
  • \d matches one or more numeric digits
  • $ matches the end of the string
  • Related