Your last line needs to be $b.Trim(). ☺
But if you want to go old-school about it:
$str.Remove( $str.IndexOf( '(' ), $str.IndexOf( ')' ) - $str.IndexOf( '(' ) +
1).Trim()
Of course, I would never put such an abomination into production, primary
because it assumes that both ( and ) exist. But I would do this, without
hesitation:
$startIndex = $str.IndexOf( ‘(‘ )
$stopIndex = $str.IndexOf( ‘)’ )
$result = $str
if( $startIndex –ge 0 –and $stopIndex –ge 0 )
{
$result = $str.Remove( $startIndex, $stopIndex - $startIndex + 1
).Trim()
}
$result
I’m forced to say, after 35 years in IT, I still can only write the simplest of
regexes. Perhaps I’ll learn how before I retire. ‘Cuz Devin’s solution is very
slick.
From: [email protected] [mailto:[email protected]] On
Behalf Of Scott Crawford
Sent: Wednesday, November 30, 2016 4:31 PM
To: [email protected]
Subject: RE: [powershell] Remove anything between defined characters:
Which is much better than my way, but here it is anyway ☺ since it takes a
different approach.
$a = "Jessica (Yvonne)"
$b = ""
for ($i = 0; $i -lt $a.length; $i++) {
if ($a[$i] -eq "(") {$Delete = $true}
if ($a[$i] -eq ")") {$Delete = $false}
if (-not $Delete -eq $true -and $a[$i] -ne ")") {$b = $b + $a[$i]}
}
$b.TrimEnd()
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Devin Rich
Sent: Wednesday, November 30, 2016 3:15 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] Remove anything between defined characters:
I would do it this way because i'm lazy:
-replace " ?\(.*?\) ?"
PS > "(ywfew) Jessica (Yvs)" -replace " ?\(.*?\) ?"
Jessica
PS > "(ywfew)Jessica (Yvs)" -replace " ?\(.*?\) ?"
Jessica
PS > "Jessica (Yvs)" -replace " ?\(.*?\) ?"
Jessica
Thanks,
Devin Rich
Systems Administrator
On Wed, Nov 30, 2016 at 2:00 PM, Orlebeck, Geoffrey
<[email protected]<mailto:[email protected]>>
wrote:
I’m working on a script that gets some data fed into it. Some of the fields
have two names, one wrapped in (). Example: “Jessica (Yvonne)”. I am trying to
remove everything within the (), but I’m not sure how. I’ve tried several regex
expressions, and I’m able to remove the (), but not the content within them.
Essentially I’d like to turn “Jessica (Yvonne)” into “Jessica” (even if there
is whitespace after ‘Jessica’).
I tried several suggestions I saw online, escaping the “(“ and “)”, or using
$regex = [regex]::Escape('()'), but I’m not sure how to basically say “Find ‘(‘
and remove everything until ‘)’ [including the parenthesis].
Any helpful tips or links where people are doing this? I tried doing array
matches, removing the () and then selecting only the first position, but then
discovered some of the fields have the parenthesis first (e.g. “(Yvonne)
Jessica”.
Thanks for any suggestions.
Confidentiality Notice: This is a transmission from Community Hospital of the
Monterey Peninsula. This message and any attached documents may be confidential
and contain information protected by state and federal medical privacy
statutes. They are intended only for the use of the addressee. If you are not
the intended recipient, any disclosure, copying, or distribution of this
information is strictly prohibited. If you received this transmission in error,
please accept our apologies and notify the sender. Thank you.
The information contained in this message is privileged, confidential, and
protected from disclosure. If you are not the intended recipient, you are
hereby notified that any review, printing, dissemination, distribution, copying
or other use of this communication is strictly prohibited. If you have received
this communication in error, please notify us immediately by replying to the
message and deleting it from your computer.