I want to match a string when it contains anything but not "just spaces".
The spaces are in order and can be anywhere while there is something else.
I can't seem to get a match when space appears anywhere.
(EDIT: I want to do this in a 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(); } }
What gives this conclusion:
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=''
I want it:
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