Sudheej
Sudheej

Reputation: 2029

RegEx Substring Extraction Powershell

How do i extract the first instance of the below pattern from string.

$a="CIC-284 Updated code to reflect changes from Defect 46"

$a -match "CIC-*"
True

Expected result : CIC-284 from the string using powershell.

Upvotes: 1

Views: 72

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

Your regular expression (CIC-*) matches the character sequence "CIC" followed by any number of hyphens. The asterisk is a quantifier with the meaning "zero or more times the preceding expression".

To match the character sequence "CIC-" followed by any number of digits change your expression to CIC-\d*. \d is an escape sequence that matches digits. Alternatively you can use [0-9] instead of \d.

Upvotes: 1

Related Questions