I want to match a string when it contains anything but not 'only spaces'.
Spaces are fine, and can be anywhere as long as there is something else.
I can't seem to get a match when a space appears anywhere.
(EDIT: I am looking to do this in regex as I ultimately want to combine it with other regex patterns using | )
Here is my test code:
class Program
{
static void Main(string[] args)
{
List<string> strings = new List<string>() { "123", "1 3", "12 ", "1 " , " 3", " "};
string r = "^[^ ]{3}$";
foreach (string s in strings)
{
Match match = new Regex(r).Match(s);
Console.WriteLine(string.Format("string='{0}', regex='{1}', match='{2}'", s, r, match.Value));
}
Console.Read();
}
}
Which gives this output:
string='123', regex='^[^ ]{3}$', match='123'
string='1 3', regex='^[^ ]{3}$', match=''
string='12 ', regex='^[^ ]{3}$', match=''
string='1 ', regex='^[^ ]{3}$', match=''
string=' 3', regex='^[^ ]{3}$', match=''
string=' ', regex='^[^ ]{3}$', match=''
What I want is this:
string='123', regex='^[^ ]{3}$', match='123' << VALID
string='1 3', regex='^[^ ]{3}$', match='1 3' << VALID
string='12 ', regex='^[^ ]{3}$', match='12 ' << VALID
string='1 ', regex='^[^ ]{3}$', match='1 ' << VALID
string=' 3', regex='^[^ ]{3}$', match=' 3' << VALID
string=' ', regex='^[^ ]{3}$', match='' << NOT VALID
Thanks