1

The goal is to get only the leading digits before a SPACE character from a string. When I use -replace in PowerShell, leading alphabetic characters are included. Why is this different?

PS C:\src\t> 'aasdf123 456' -replace '([\d]+) .*', '$1'
aasdf123

This does not occur on regex101.com which produces.

123

Not a duplicate

The question is not how to get the number. The question is why does 'aasdf' match \d. The capture group specifies only \d.

lit
  • 14,456
  • 10
  • 65
  • 119

1 Answers1

0

The behavior is expected.

The -replace operator removes all non-overlapping matches from the input string keeping all non-matched parts in the result.

The 'aasdf123 456' -replace '([\d]+) .*', '$1' will return assdf123 because the ([\d]+) .* (equal to (\d+) .*) matches and captures into Group 1 one or more digits, then matches a space and then the rest of the line, and then the whole match (here, 123 456) is replaced with the contents of Group 1, 123, thus, keeping assdf123 as the result of the -replace operation.

Your statement that regex101 produces another result is wrong: it produces the same result:

enter image description here

Even if you look at the MATCH INFORMATION pane, there are two values: the whole match, 123 456, and the Group 1 value, 123. So, there is no discrepancy at all.

To get just 123, you may use the solution described in How to get the captured groups from Select-String?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I see that using -match works. I would like to mark this as the answer. But, I would like to know how `assdf` got into a group containing only `\d`. – lit Feb 24 '19 at 20:48
  • @lit Please see *The `'aasdf123 456' -replace '([\d]+) .*', '$1'` will return `assdf123` because the `([\d]+) .*` (equal to `(\d+) .*`) matches and captures into Group 1 one or more digits, then matches a space and then the rest of the line, and then the whole match (here, `123 456`) is replaced with the contents of Group 1, `123`, thus, keeping `assdf123` as the result of the `-replace` operation.* `assdf` *NEVER* got into any group, it **was not matched**. It is clearly seen on the regex101 screenshot: that part of string is not highlighted. – Wiktor Stribiżew Feb 24 '19 at 20:57
  • And again: *The `-replace` operator removes all non-overlapping matches from the input string **keeping all non-matched parts in the result**.* – Wiktor Stribiżew Feb 24 '19 at 20:58
  • Ok. I am marking this as the answer. It looks like that since 'aasdf' was not matched, it was kept in the output result. I still have to think about this some more. – lit Feb 25 '19 at 00:54