Regular expression for replacing the strings in powershell

tjdoubts

I have certain strings as given below

PS C:\Users> $S1="PS CUT (ZIP ONLY),  ALWAYS chose for MARTIO"
PS C:\Users> $s2="ZINO-IAS CUT"
PS C:\Users> $s3="ZINO-IPS CUT 2"
PS C:\Users> $s4="ZINO-IAS CUT4"
PS C:\Users> $s5="ZINO-IPS"

I want to replace above strings from the word CUT itself as given below

PS
ZINO-IAS
ZINO-IPS2
ZINO-IAS4
ZINO-IPS

Plase not that if there is a number after word CUT we are not removing that.

i tried the following code but it doesn't worked

PS C:\Users> $s5 -replace "CUT\s*\w*",""
arco444

The regex you need is:

"\s*CUT\s*(\d+)?.*","`$1"

It checks for an optional number that comes after CUT and any spaces and uses that as the replacement (represented by $1). If the number doesn't exist, it replaces the entire match with an empty string.

Result:

[PS]> $s1 -replace "\s*CUT\s*(\d+)?.*","`$1"
PS
[PS]> $s2 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IAS
[PS]> $s3 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IPS2
[PS]> $s4 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IAS4
[PS]> $s5 -replace "\s*CUT\s*(\d+)?.*","`$1"
ZINO-IPS

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

regular expression matching and replacing pvalue strings

From Dev

Java Regular expression for replacing specific strings

From Dev

Replacing a String with Regular expression

From Dev

Regular Expression Matching and Replacing

From Dev

VBA regular expression, replacing groups

From Dev

VBA regular expression, replacing groups

From Dev

replacing characters with javascript regular expression

From Dev

Nested regular expression strings

From Dev

Regular Expression on Strings

From Dev

Regular expression for set of strings

From Dev

Powershell Regular Expression

From Dev

powerShell and regular expression

From Dev

Regular expression in Powershell

From Dev

Python Regular Expression; replacing a portion of match

From Dev

Replacing a string without using regular expression in netezza

From Dev

Regular expression for replacing a specific phone number

From Dev

XPath/XQuery Regular Expression replacing a String pattern

From Dev

Regular expression for replacing a specific phone number

From Dev

Regular expression for replacing special characters with underscore

From Dev

Excel VBA - Replacing Values with a Regular Expression Function

From Dev

Regular Expression to find uncommented strings?

From Dev

Regular expression strings with consecutive variables

From Dev

regular expression to check array of strings

From Dev

Regular expression for matching numbers and strings

From Dev

Regular expression with URL Encoded Strings

From Dev

Regular expression for strings not containing brackets

From Dev

Regular Expression Nested Similar Strings

From Dev

How to write regular expression in powershell

From Dev

Powershell - Regular Expression Multiple Matches

Related Related

HotTag

Archive