[Mono-list] Regex dosen't recognize \s or \w ?

Weeble clockworksaint at gmail.com
Wed Feb 4 19:23:26 UTC 2015


On 4 February 2015 at 15:03, mickeyf <mickey at thesweetoasis.com> wrote:
> I am using Monodevelop 3.0.3.2,  with mono runtime 2.10.8.1 on Ubuntu 12.04
>
> When I try to include \s or \w in my regex, I get 'Unrecognized escape
> sequence', Otherwise regex works as I'd expect. (But I freely admit to
> having limited experience with regex.)
>
> What have I missed? Or if \s is not available, how do I specify "zero or
> more spaces" ?

It works fine for me. Here's a short example:

using System;
using System.Text.RegularExpressions;

public class Program
{
    static void Main()
    {
        Regex r = new Regex(@"^a\s*b$");
        Action<string> testMatch =
            (string s) =>
                Console.WriteLine(
                    "\"{0}\": {1}",
                    s,
                    r.Match(s).Success ? "Matches" : "Does not match");
        testMatch("a    b");
        testMatch("a    c");
        testMatch("ab");
        testMatch("ac");
        testMatch("a\tb");
        testMatch("a\tc");
    }
}

Have you forgotten to escape the backslashes in your string literal?
Are you seeing that error at compile-time? If so, it has nothing to do
with regular expressions. It means that you've broken the C# syntax
for writing strings. You either need to use a C# verbatim string,
which has an at-sign (@) in front of it, or you need to escape each
backslash by prefixing it with another backslash. See
https://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx

Hope that helps.

Weeble.


More information about the Mono-list mailing list