views:

134

answers:

3

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).

+1  A: 

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.

Jon Skeet
Your code works fine for me, as well. It even matches when I copy-and-paste the string to match directly from my own code, which I suppose rules out the non-printing character theory. My program still fails, however. The only difference is that I'm checking match.Success after calling regex.Match, rather than calling regex.IsMatch. Could that have something to do with it? As for your second suggestion, only a single character should be allowed.
David Brown
Nevermind, I found the problem. I messed up an if statement in some other code that ended up stripping all whitespace from each line before it was matched against the regex. I changed it to only strip whitespace outside of single quotes and now it works. Checking for a non-printing character is a good suggestion for other cases, though, so I'll mark this as the answer.
David Brown
A: 

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

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

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

The Matt