[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,  with mono runtime 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) =>
                    "\"{0}\": {1}",
                    r.Match(s).Success ? "Matches" : "Does not match");
        testMatch("a    b");
        testMatch("a    c");

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

Hope that helps.


More information about the Mono-list mailing list