question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RegEx Pattern works on regex101.com but does not work with select-string -pattern

See original GitHub issue

Prerequisites

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

(https://regex101.com/r/Itarw8/1)

Issue Analytics

  • State:open
  • Created 3 months ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mklement0commented, Jun 8, 2023

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).

    • A notable difference between PowerShell-mediated regex functionality vs. direct .NET API use is that PowerShell uses case-insensitive regexes by default.
  • With respect to using regex101.com - a great tool, to be sure - for PowerShell, there are pitfalls:

    • It assumes different defaults (even if you choose the NET (C#) flavor): notably, the m (Multiline) option is on by default, and i (CaseInsensitive) is off by default; the g (global) option, which has no direct .NET equivalent is on by default, but it doesn’t apply to match or switch -Regex, for instance.
    • It assumes (verbatim) C# string-literal syntax, requiring escaping of " chars. as ""
    • See https://github.com/firasdib/Regex101/issues/1838 for a request to support a PowerShell-specific flavor that would eliminate these problems.
    • However, two pitfalls will remain:
      • The site only supports a single, multiline input string to test against.

        • This notably contrasts with PowerShell often reporting lines as a stream of multiple, individual (newline-free) strings, as when you use Get-Content (without -Raw) and when you process output from external programs, such as vssadmin.exe in your case.
        • As @iRon7 has pointed out, piping to Out-String is the simplest solution for external programs; for Get-Content, it is -Raw.
        • Also note that Select-String, when given a file (file-info object, such as output by Get-ChildItem) as input also searches line by line; searching across lines requires an idiom such as Get-Content -Raw ... | Select-String ...
      • Pasting multi-line strings as subject strings (field “TEST STRING”) invariably converts CRLF (Windows-format) to LF (Unix-format) newlines.

<div> GitHub</div><div>Issues · dotnet/runtime</div><div>.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - Issues · dotnet/runtime</div>
1reaction
B-Artcommented, Jun 8, 2023

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found