Removing specific strings from a variable

Hi,

I’ve read last month a way to do what I want, but I lost the article and can’t find it =/

I have a variable with this information gathered from a text file using Get-Content:

Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)

I want the variable to have only this:

Carl Marx; William Shakespeare; Paul Newman; Bruce Willis

How can I remove the “(mail@contoso.com)” information easily?

$var = $var -replace " ;“,”;"

Would be one way.

Don Jones,

Sorry, you must read the post as I’ve been trying to insert the (mail@contoso.com) text…
I was trying using "<" to insert…

This example should work. Copy and paste the part you need (remove mail or add mail).

$string = "Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)"

# Remove mail for each
$removemail = $string -split ';' -replace "\(.*\)",";"
$removemail.Trim()
# Result:  Carl Marx ;

# Add mail for each
$addmail = $string -split ';' -replace "@",".mail@"
# Result: Carl Marx (carl.marx.mail@contoso.com)

random commandline,

Thanks! Worked perfectly =]

Just something a little more simple. You can use just -replace with RegEx for this.

$input = "Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)"
$input -replace " \(.*?\)"

Results:

Carl Marx; William Shakespeare; Paul Newman; Bruce Willis