Question

I have the following regular expression:

([0-9]+),'(.)':([0-9]+),(L|R|'.')

It matches this just fine:

1,'a':1,R

However, if I replace a with a space, it fails:

1,' ':1,R

Why doesn't . match it? Is a space not classified as a character? I can't use \s because I don't want to match tabs and line breaks. I also tried:

([0-9]+),'(.| )':([0-9]+),(L|R|'.')

But that doesn't work, either (and I don't have IgnorePatternWhitespace enabled).

Was it helpful?

Solution

I can't reproduce what you're seeing:

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        Regex regex = new Regex("([0-9]+),'(.)':([0-9]+),(L|R|'.')");
        Console.WriteLine(regex.IsMatch("1,' ':1,R"));
    }
}

prints "True".

Is it possible that you've got another character between the quotes as well? Some non-printing character? Where is the text coming from?

You could try changing it to:

([0-9]+),'([^']+)':([0-9]+),(L|R|'.')

so it could match more than one character between the quotes.

OTHER TIPS

I haven't tried in .NET, but the dot is language and implementation specific. Try:

([0-9]+),'([.| ])':([0-9]+),(L|R|'.')

Use \0x0020 which will match a single space character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top