Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match anything but 'just spaces' using .Net Regex

Tags:

c#

regex

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

like image 869
nuxibyte Avatar asked Dec 12 '22 17:12

nuxibyte


2 Answers

I'd use

^\s*\S+.*?$

Breaking down the regex...

  • ^ - start of line
  • \s* - zero or more whitespace characters
  • \S+ - one or more non-whitespace characters
  • .*? - any characters (whitespace or not - non-greedy -> match as few as possible)
  • $ - end of line.
like image 146
Grhm Avatar answered Jan 11 '23 09:01

Grhm


No need for a regular expression here. You can use string.IsNullOrWhitespace()

A regular expression is this:

[^ ]

What this does is simple: It checks if your string contains anything that is not a space.

I adjusted your code slightly by adding match.Success to the output:

var strings = new List<string> { "123", "1 3", "12 ", "1  " , "  3", "   ", "" };

string r = "[^ ]";
foreach (string s in strings)
{
    Match match = new Regex(r).Match(s);
    Console.WriteLine(string.Format("string='{0}', regex='{1}', match='{2}', " +
                                    "is match={3}", s, r, match.Value,
                                    match.Success));
}

The result will be:

string='123', regex='[^ ]', match='1', is match=True
string='1 3', regex='[^ ]', match='1', is match=True
string='12 ', regex='[^ ]', match='1', is match=True
string='1  ', regex='[^ ]', match='1', is match=True
string='  3', regex='[^ ]', match='3', is match=True
string='   ', regex='[^ ]', match='', is match=False
string='', regex='[^ ]', match='', is match=False

BTW: Instead of new Regex(r).Match(s) you should use Regex.Match(s, r). This allows the regex engine to cache the pattern.

like image 40
Daniel Hilgarth Avatar answered Jan 11 '23 09:01

Daniel Hilgarth