c++regexvalidationnetbios

NetBIOS Name Regular Expression


I have a question according to this link http://support.microsoft.com/kb/188997 ( A computer name can be up to 15 alphanumeric characters with no blank spaces. The name must be unique on the network and can contain the following special characters: ! @ # $ % ^ & ( ) - _ ' { } . ~

The Following characters are not allowed: \ * + = | : ; " ? < > , )

and I am developing in C++

so i used the following code but when i input character which isn't allowed.. it is matched ! why ?

 regex  rgx("[a-zA-Z0-9]*(!|@|#|$|%|^|&|\(|\)|-|_|'|.|~|\\{|\\})*[a-zA-Z0-9]*");


string name;
    cin>>name;

if (regex_match(name, rgx))
{
    cout << " Matched :) " << endl;
}
else
    cout << "Not Matched :(" << endl;

your help will be greatly appreciated :)


Solution

  • Your regular expression will match any string, because all your quantifiers are "none or more characters" (*) and since you're not looking for start and end of the string, you'll match even empty strings. Also you're using an unescaped ^ within one pair of brackets ((...|^|...), which will never match, unless this position is the beginning of a string (which may happen due to the *quantifier as explained above).

    It's a lot more easier to achieve what you're trying to though:

    regex rgx("^[\\w!@#$%^()\\-'{}\\.~]{1,15}$");
    

    If you're using C++11, you might as well use a raw string for better readability:

    regex rgx(R"(^[\w!@#$%^()\-'{}\.~]{1,15}$)");
    

    This should match all valid names containing at least one (and up to) 15 of the selected characters.