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 :)
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.
\w
matches any "word" character, that is A-Z, a-z, digits, and underscores (and based on your locale and regex engine possibly also umlauts and accented characters). Due to this it might be better to actually replace it with A-Za-z\d_
in the above expression:
regex rgx("^[A-Za-z\\d_!@#$%^()\\-'{}\\.~]{1,15}$");
Or:
regex rgx(R"(^[A-Za-z\d_!@#$%^()\-'{}\.~]{1,15}$)");
{a,b}
is a quantifier matching the previous expresssion between a and b times (inclusive).
^
and $
will force the regular expression to fill the whole string (since they'll match beginning and end).