Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am doin strin validation ,when ever user give input as below characters i ahve throw error like not valid string.
" / \ [ ] : ; | = , + * ? < >

but i am getting error as
"Microsoft C++ exception: std::regex_error at memory location 0x000000000019A210."

What I have tried:

bool ValidateLoginid(CString inputString)
{std::regex stringpattern("[!/\[]:;|=,+*<>]*");

std::string s_input(CW2A(inputString.GetString()));
if (std::regex_match(s_input, stringpattern))
{
return false;
}

return true;


}

could you please suggest me if anything is wrong here
Posted
Updated 25-Apr-18 21:54pm

Look at your regex:
[!/\[]:;|=,+*<>]*
It contains the characters to start and end the "any character in this set" within the set of characters, as well as the Escape character!
Try this:
[!/\\[\]:;|=,+*<>]*

But... that will match no special characters at all - try this:
[!/\\[\]:;|=,+*<>]+

Oh, and you probably need to double each '\' character in your C++ string, so that it doesn't get "swallowed" as the C++ Escape character:
std::regex stringpattern("[!/\\\\[\\]:;|=,+*<>]+");


If you want to use regular expressions, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v3
There are a few problems with your string:

  • The backslash inside your character class isn't escaped, so \[ is being treated as escape sequence, but it doesn't exist. And if you'd escape the backslash like \\, so it would 'end up' being a single backslash, the regex will interpret that as 'the next character is meant to be a literal character', so you have to add another backslash, and, in your string, this will look like another double backslash. So for a literal backslash in the regex, you need 4 backslashes in your string.
  • The ] in the character class ends the character class, but you want it to 'go on', so you have to escape the ]. In regular expression, that would be \], but because you need to escape the backslash in the string, it becomes \\].

So it becomes:
C++
std::regex stringpattern("[!/\\\\[\\]:;|=,+*<>]*");

But we aren't done yet! This code won't do what you want: return false if the string contains one invalid character. The reason is that regex_match matches the full string, whereas you want to search. Instead of regex_match, use regex_search.

And one last thing: the * after the character class means 'zero or more'. That's not what you want: you want 'one or more'. You can replace the * by a + but because you're searching, you can also just remove the *, so your code becomes:
C++
std::regex stringpattern("[!/\\\\[\\]:;|=,+*<>]");

// ...

if (std::regex_search(s_input, stringpattern))
{
    return false;
}
return true;
 
Share this answer
 
v2
Comments
Member 13089825 4-Aug-17 5:55am    
its throwing error when im giving those charcters alone
but when i enetred like below
test>
<]test
>test]++
its not throwing error but i need to throw error even inthat case also(my string should not contain those charactes)
could you please suggest me
i have tried like

bool ValidateLoginid(CString inputString)
{
//..std::regex stringpattern("[a-zA-Z0-9][0-9a-zA-Z_!@#$%^&()']*");
std::regex stringpattern("[!/\\\\[\\]:;|=,+*<>]*");

std::string s_input(CW2A(inputString.GetString()));
if (std::regex_match(s_input, stringpattern))
{
return false;
}

return true;


}
Thomas Daniels 4-Aug-17 5:57am    
I'm not getting an error if I try that, but my testing code is a bit more 'minimalistic' than your code... What does s_input look like at the end? What does CW2A do with it?
That is because you forgot to escape the closing square bracket in the character group and that they must be double escaped (so that a backslash is placed in the string to be seen by the regex parser):
wrong:   "[!/\[]:;|=,+*<>]*"
correct: "[!/\\[\\]:;|=,+*<>]*"

You also don't need to convert the input to ANSI when using the wregex class provided by Visual C++:
bool ValidateLoginid(const CString& inputString)
{
    std::wregex stringpattern(L"[!/\\[\\]:;|=,+*<>]*");
    return !std::regex_match(inputString.GetString(), stringpattern);
}
Note also that I have changed the parameter to be a const reference to avoid passing by value and indicate that it is not changed.

There is also no need to use a regular expression if you only want to check if specific characters are present or not. Then just use strstr or CString::FindOneOf.
 
Share this answer
 
v2
Comments
Member 13089825 4-Aug-17 6:09am    
hi i have tried this,but its not thorwing error when i give test+,test>(if i give combination then its not throwing error)
bool ValidateLoginid(const CString& inputString)
{
std::wregex stringpattern(L"[!/\\[\\]:;|=,+*<>]*");
return !std::regex_match(inputString.GetString(), stringpattern);
}
Jochen Arndt 4-Aug-17 6:20am    
As also suggested by others, you should replace the last match char '*' by '+' or '+?'.
I can't actually test it here and have written my solution from mind.

However, I would just use
if (inputString.FindOneOf(_T("!/[]:;|=,+*<>")) >= 0)
// error: string contains unallowed character
Member 13089825 8-Aug-17 7:52am    
thank you,its working fine now
Member 13089825 26-Apr-18 6:44am    
wrong: "[!/\[]:;|=,+*<>]*"
correct: "[!/\\[\\]:;|=,+*<>]*"
is it possible to validate pattern is correct or not using vc++ mfc
Jochen Arndt 26-Apr-18 6:50am    
?You seemed to have stopped typing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900