manoj kumar
manoj kumar

Reputation: 49

Removing the substring

Trying to remove the substring in between the braces in powershell.

$name = "Kothuru, Manoj Kumar (Happy)"
$name = "Manoj Kumar"
$name = "Manoj (end)"

I tried the below code by splitting the string by braces.

$name.Split("(")[0]

Is there any better way to do this. Since if the name doesn't contain the tail. It won't work

Upvotes: 0

Views: 42

Answers (1)

user459872
user459872

Reputation: 24602

You can use Replacement operator. It has the following syntax.

<input> -replace <regular-expression>, <substitute>

Use \(.*?\) pattern to match the characters between the parentheses and replace it with empty string.

$name -replace "\(.*?\)", ""

Upvotes: 2

Related Questions