I have the following array and need two Regex filters that I want to use in PowerShell.
000111
010101
220220
123456
Filter 1: the number 0 that occurs equal or more than three times. I expect the following values after filtering
000111
010101
Filter 2: all numbers that occur equal or more than three times. I should only see these numbers.
000111
010101
220220
With 0{3,} I can only recognize numbers in sequence so i get only the number
000111
Is it possible to find repeating numbers that are between other numbers?
CodePudding user response:
If using just a regex pattern for each filter is even possible, it will likely be mind-bending (hard to understand).
Either way, you can use additional PowerShell features to achieve the desired results in a conceptually clear manner:
Filter 1:
@(
'000111'
'010101'
'220220'
'123456'
).Where({ ($_ -replace '[^0]').Length -ge 3 })
That is, each string in the input array (enumerated via the intrinsic Where()
method), has all chars. that aren't '0'
([^0]
) removed via the regex-based -replace
operator, and only those strings for which the resulting string has 3 or more characters, i.e. '0'
instances, are returned.
Filter 2:
@(
'000111'
'010101'
'220220'
'123456'
).Where({ ([char[]] $_ | Group-Object).Where({ $_.Count -ge 3 }, 'First') })
That is, each input string by is grouped by its characters using Group-Object
, and only those groups comprising at least 3 members are considered, with the first such group being found ('First'
) stopping the search for further groups.
If a group is returned, the enclosing .Where()
method call will include the string at hand in the output, based on PowerShell's implicit to-Boolean conversion rules, which are summarized in the bottom section of this answer.
CodePudding user response:
Since you insist to see the solution in regex, look at this: '(\d).*\1.*\1'
I think this is comprehensible without further explanation, isn't it?