Reputation: 1544
I'm looking for a RegEx expression that lets me find all instances that match a pattern.
PowerShell:
> if ("123 234 345 567" -match ".*(\d{3}).*") { $Matches | fl}
Name : 1
Value : 567
Name : 0
Value : 123 234 345 567
I'd like to also get the other three-digit values as individual matches.
-S
Upvotes: 2
Views: 619
Reputation: 626802
You can use
$s = "123 234 345 567"
Select-String '\d{3}' -input $string -AllMatches | % {$_.matches.value}
Notes:
\d{3}
pattern, you can extract three consecutive digits from any context-AllMatches
, you can extract all multiple occurrences/matches% {$_.matches.value}
will get the matched values as strings.The pattern can also be defined in other ways:
(?<!\d)\d{3}(?!\d)
pattern lets you extract three consecutive digits that are not enclosd with other digits(?<!\d\.?)\d{3}(?!\.?\d)
- to match only integer numbers consisting of 3 digits in messed-up contexts(?<!\S)\d{3}(?!\S)
- match three digit chunks in between whitespaces or start/end of string\b\d{3}\b
- match three digit numbers as whole words (not enclosed with letters, digits or underscores).Upvotes: 1