Why is this regex not matching whitespace?
I have the following regex:
([0-9]+),'(.)':([0-9]+),(L|R|'.')
It fits this simply:
1,'a':1,R
However, if I replace a with a space, it doesn't work:
1,' ':1,R
Why doesn't it match ? ? Is the space unclassified as a character? I can't use \ s because I don't want to match tabs and line breaks. I've also tried:
([0-9]+),'(.| )':([0-9]+),(L|R|'.')
But that doesn't work either (and mine doesn't IgnorePatternWhitespace
).
+2
source to share
3 answers
I cannot reproduce what you see:
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 have another character between the quotes? Some kind of unprintable character? Where is the text?
You can try changing it to:
([0-9]+),'([^']+)':([0-9]+),(L|R|'.')
so it can match multiple characters between quotes.
+1
source to share