0

I totally get how to escape back slashes, which is why this is throwing me for a loop: Regex

I have tried everything -- not using @ and manually escaping the back slashes, use the @ and escaping... nothing seems to work. What am I missing? What am I doing wrong?

I am using .NET Core 2.0.

Here is the code:

        var path = "this is a test/{lcc_root_whatever}/string done.";
        if (Regex.IsMatch(path, @"\Q/{lcc_root_\E[[:word:]]+\Q}/\E"))
        {

        }
Andy
  • 12,859
  • 5
  • 41
  • 56
  • Post a **real** code. Above code is compilable – Eser Mar 29 '18 at 20:14
  • 1
    Good news: you actually do not even need to escape anything, remove `\Q` and `\E` (that are not compatible with .NET regex). But you need to change `[[:word:]]` to `\w`. – Wiktor Stribiżew Mar 29 '18 at 20:16
  • .net uses a perl compat regex. If you don't know what that means... –  Mar 29 '18 at 20:17
  • 1
    @Will .NET does not use PCRE, it uses a .NET regex library. – Wiktor Stribiżew Mar 29 '18 at 20:18
  • @WiktorStribiżew -- answer with that and i'll accept. Thanks so much! – Andy Mar 29 '18 at 20:18
  • @WiktorStribiżew "In .NET, regular expression patterns are defined by a special syntax or language, which is compatible with Perl 5 regular expressions and adds some additional features such as right-to-left matching." https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions –  Mar 29 '18 at 20:19
  • @Will The fact that .NET regex is NFA and is similar in some way to Perl does not mean it uses Perl or PCRE syntax. Lots of engines claim they originate from Perl, it does not make them compatible with Perl or PCRE. – Wiktor Stribiżew Mar 29 '18 at 20:20
  • 1
    @WiktorStribiżew again, I said ".net uses a perl compat regex". The docs specifically states that it is compatible with Perl 5 regexes, but includes more features. I didn't say it uses Perl. –  Mar 29 '18 at 20:22
  • @Will Mentioning *Perl x compatible* in the .NET regex context is inappropriate as the current .NET regex is not quite compatible with any Perl x regex. It is more appropriate to say .NET regex originated from Perl regex, or Perl regex syntax was the source of inspiration for .NET regex library authors. – Wiktor Stribiżew Mar 29 '18 at 20:25
  • 1
    @WiktorStribiżew JFC the docs specifically says it's Perl 5 compatible. Unless you're on the regex team at Microsoft, the docs win. Good day to you, sir. –  Mar 29 '18 at 20:27
  • hey now -- no reason to downvote my question! It was an honest question. I didn't understand that `/Q` was an invalid token -- as a matter of fact it said it was an invalid escape sequence when, in fact, it wasn't an invalid escape sequence. I didn't understand the exception. – Andy Mar 29 '18 at 20:36

1 Answers1

0

You actually do not even need to escape anything, remove \Q and \E (that are not compatible with .NET regex). You still need to change [[:word:]] to \w.

Use

if (Regex.IsMatch(path, @"/{lcc_root_\w+}/"))
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions – screig Mar 29 '18 at 20:20