Home

Find and Replace with Regex seems broken

edited January 2023

Regex find and replace in LINQPad doesn't seem to use standard regex syntax. I'm having to escape my quotes and can't use question marks in my expressions.

Here's the code I'm working with:

public class Something
{
    public string[] Strings { get; set; } = { "One", "Two", "Three" };
    public string Name { get; set; }
}

And the pattern: ^(\s+)public .*? (\w+).*?"(.*?)"

When I click find, I get the "specified text was not found" dialog.

If I remove the question marks and escape the quotes, it works, but without the question marks, the capture groups are incorrect:

Regex in .net works as expected:

var code = @"public class Something
{
    public string[] Strings { get; set; } = { ""One"", ""Two"", ""Three"" };
    public string Name { get; set; }
}".Dump("Code");

var pattern = @"^(\s+)public .*? (\w+).*?""(.*?)""".Dump("Pattern");

Regex.Match(code, pattern, RegexOptions.Multiline).Dump("Match");

Regex.Replace(code, pattern, "\n\n1: $1\n2: $2\n3: $3\n\n", RegexOptions.Multiline).Dump("Replace");

I'm using LIQNPad 7.6.6

Comments

Sign In or Register to comment.