You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ideas for a clean, direct regex mirror of -replace, to "keep the match"? #7958
An example regex thing I do is to extract the number out of this string: "blah blah blah 456 blah blah".
The available options are all full of syntax, noise, and multiple steps:
if ($text -match '(\d+)') { $matches[1] }
$text | select-string '\d+' | % matches | % value
[regex]::matches($text, '\d+').Value
$text -replace '.*?(\d+).*', '$1'
But if I wanted to /drop/ the number from the string, there's a really clean short direct way to do it:
$text -replace '\d+'
NB. that in this example I could simply use $text -replace '[^\d]' , but coming up with an inverted regex often is not that simple. Especially if there's a string involved, so an inverted character class won't work. It often means separately matching the start and end, andleaving the middle bit unchanged, as I've tried to show in example number 4 above.
This is a pattern I find myself using quite often in the shell, and it bugs me that there's a first class way to get rid of, and swap string content, but not exactly a first class mirror of that, a way to keep some string content.
What I'm dreaming of, is an operator that works like the opposite of -replace, like this:
$text -keep '\d+'
An operator which behaves like [regex]::matches($text, '\d+').Value - i.e. the result would be plain strings, not MatchInfo, not $matches being set somewhere off in memory. If there's one match there's one string output. If there's N matches, then N strings output. No position, no metadata, no nesting. If there's no matches, there's no output (it doesn't return $false). If it's used on an array of strings on the left, it will output the matches from each of them, not filtering the array.
Maybe it could take another parameter like -split does to return only the first N items, so
changed the title [-]Ideas for a clean, direct regex way to "keep the match"?[/-][+]Ideas for a clean, direct regex mirror of `-replace`, to "keep the match"?[/+]on Oct 7, 2018
changed the title [-]Ideas for a clean, direct regex mirror of `-replace`, to "keep the match"?[/-][+]Ideas for a clean, direct regex mirror of -replace, to "keep the match"?[/+]on Oct 7, 2018
I wrote this independently before I saw issue #7867. That is still about outputting a chunk of metadata, which is not what I want, but there is talk in the comments of string output, I will chase this up there instead.
But if I wanted to /drop/ the number from the string, there's a really clean short direct way to do it:
*(I realised later that you are not looking for a way to drop the number, it was just an example that dropping is easy; expand for some historic stuff that is probably unnecessary)*
You are pretty close with $text -replace '.*?(\d+).*', '$1'. Have you considered:
PS C:\> $text -replace '(.*?)(\d+)(.*)', '$1$3'
i.e. Capture the prefix as Group 1, the number as Group 2 and the suffix as Group 3; then replace that whole match (prefix through to suffix) with the content of Group1 and Group 3
This tends to work best when there is only one match in the string, as the subsequent matches get swallowed by the suffix capture.
Alternatively, an approach I sometimes like is the split/join:
PS C:\> ($text -split '\d+') -join ''
i.e. Split the source string into an array of substrings, using your expression as the separator, then rejoin the array of substrings using an empty string as the separator.
I believe it has the effect you are looking for, but it depends how performant you need to be. On a large source data, split/join will probably allocate a significant number of strings.
Of course, if you just want to drop all numbers from the input, the sanest and simplest is still...
PS C:\> $text -replace '\d+',''
i.e. Replace all instances of the number with nothing.
If you are looking for a sane way to keep only the strings that match your pattern, you probably need to write a function. Something like...
This is a different problem, and assumes too much about the input string. Unlike the first request, this time you presume that the string should be automatically split on whitespace before it is treated as a list of terms, and only the matching terms should be returned, rather than a combined string.
If you want significant whitespace to cause the string to split, you should have to say so, and there are already good-enough idiomatic ways to achieve that, e.g...
If there were a -keep operator that behaved as the inverse of -replace , I would expect such an operator to actually return the string, as -replace does...
Activity
[-]Ideas for a clean, direct regex way to "keep the match"?[/-][+]Ideas for a clean, direct regex mirror of `-replace`, to "keep the match"?[/+][-]Ideas for a clean, direct regex mirror of `-replace`, to "keep the match"?[/-][+]Ideas for a clean, direct regex mirror of -replace, to "keep the match"?[/+]HumanEquivalentUnit commentedon Oct 7, 2018
I wrote this independently before I saw issue #7867. That is still about outputting a chunk of metadata, which is not what I want, but there is talk in the comments of string output, I will chase this up there instead.
jimbobmcgee commentedon Mar 13, 2024
*(I realised later that you are not looking for a way to drop the number, it was just an example that dropping is easy; expand for some historic stuff that is probably unnecessary)*
You are pretty close with
$text -replace '.*?(\d+).*', '$1'. Have you considered:i.e. Capture the prefix as Group 1, the number as Group 2 and the suffix as Group 3; then replace that whole match (prefix through to suffix) with the content of Group1 and Group 3
This tends to work best when there is only one match in the string, as the subsequent matches get swallowed by the suffix capture.
Alternatively, an approach I sometimes like is the split/join:
i.e. Split the source string into an array of substrings, using your expression as the separator, then rejoin the array of substrings using an empty string as the separator.
I believe it has the effect you are looking for, but it depends how performant you need to be. On a large source data, split/join will probably allocate a significant number of strings.
Of course, if you just want to drop all numbers from the input, the sanest and simplest is still...
i.e. Replace all instances of the number with nothing.
If you are looking for a sane way to keep only the strings that match your pattern, you probably need to write a function. Something like...
In theory, the changes to $Matches should be scoped to inside the function only, and should revert at the end of the function call.
This is a different problem, and assumes too much about the input string. Unlike the first request, this time you presume that the string should be automatically split on whitespace before it is treated as a list of terms, and only the matching terms should be returned, rather than a combined string.
If you want significant whitespace to cause the string to split, you should have to say so, and there are already good-enough idiomatic ways to achieve that, e.g...
If there were a
-keepoperator that behaved as the inverse of-replace, I would expect such an operator to actually return the string, as-replacedoes...That's easy enough to add to the function...