RegEx Pattern works on regex101.com but does not work with select-string -pattern
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
$vssadmin = vssadmin list writers ($vssadmin | Select-String -Pattern “(?msi)^Writer name: (?‘writer’.*)\r?\n Writer Id: (?‘id’[^:])\r?\n Writer Instance Id: (?‘instance’[^:])\r?\n State: (?‘state’[^:])\r?\n Last error: (?‘err’[^:])\r?\n$” -AllMatches ).Foreach{$_}
Expected behavior
Named groups of matches. writer, is, instance, state and err.
Actual behavior
Empty string
Error details
No error
Environment data
Name Value
---- -----
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
Issue Analytics
- State:
- Created 3 months ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Regular expression works on regex101.com, but not on prod
1 Answer. Make sure you always select the right regex engine at regex101.com. See an issue that occurred due to using a JS-only...
Read more >My regex isn't working but works in regex builder and ...
My regex is working in regex builder and regex101 but it's not returning any values in my sequence. I am trying to get...
Read more >Why is my regex pattern not working - Questions & Answers
First, you shouldn't use spaces, but escaped characters. But I don't know where you use that, so it might work. There isn't must...
Read more >Powershell: The many ways to use regex
Select-String ... This cmdlet is great for searching files or strings for a text pattern. ... This example searches all the files in...
Read more >Why do regular expressions that work on regex101.com ...
In PCRE regex's ^ anchor the pattern at the beginning of a string. /foo/. matches anywhere /^foo/. Matches only at the beginning of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
On a general note, @B-Art:
With respect to regexes, PowerShell is just the facilitator: the .NET regex engine does all the work.
Unless you have reason to believe that there is a problem with how PowerShell facilitates this access (which would require showing a PowerShell statement that behaves differently from the equivalent direct .NET API call), a potential bug with respect to regex processing should be reported at https://github.com/dotnet/runtime/issues (I don’t think there is one here).
With respect to using regex101.com - a great tool, to be sure - for PowerShell, there are pitfalls:
m
(Multiline
) option is on by default, andi
(CaseInsensitive
) is off by default; theg
(global) option, which has no direct .NET equivalent is on by default, but it doesn’t apply tomatch
orswitch -Regex
, for instance."
chars. as""
The site only supports a single, multiline input string to test against.
Get-Content
(without-Raw
) and when you process output from external programs, such asvssadmin.exe
in your case.Out-String
is the simplest solution for external programs; forGet-Content
, it is-Raw
.Select-String
, when given a file (file-info object, such as output byGet-ChildItem
) as input also searches line by line; searching across lines requires an idiom such asGet-Content -Raw ... | Select-String ...
Pasting multi-line strings as subject strings (field “TEST STRING”) invariably converts CRLF (Windows-format) to LF (Unix-format) newlines.
@mklement0 Thank you for your excellent explanation. I am aware that lots of functionality in PowerShell leans on .Net. For instance -Replace is completely different to .Replace(,) I know. -Replace can use Regex while .Replace(,) cannot.
I use PowerShell almost every day and I will keep on using it.
Again, thanks for your reply.