用户输入的字符串验证 [英] String validation for user input

查看:83
本文介绍了用户输入的字符串验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行strin验证,当用户输入以下字符时我会抛出错误,就像无效的字符串一样。

/ \ [] :; | =,+ *?< >



但是我收到了错误

Microsoft C ++异常:std :: regex_error在内存位置0x000000000019A210。



我尝试过:



bool ValidateLoginid(CString inputString)

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



std :: string s_input(CW2A(inputString.GetString()));

if(std :: regex_match(s_input,stringpattern))

{

返回false;

}



返回true;





}



如果此处有任何问题,请指教我

解决方案

看看你的正则表达式:

 [!/ \ []:; | =,+ *<>] * 

它包含字符集开始和结束字符集中的此集中的任何字符,以及Escape字符!

试试这个:

 [!/ \\ [\]:; | =,+ *<>] * 



但是......那将不匹配特殊字符 - 试试这个:

 [!/ \\ [\]:; | =,+ *<>] + 



哦,您可能需要将C ++字符串中的每个'\'字符加倍,以免它被吞噬为C ++ Escape字符:

 std :: regex stringpattern([!/ \\\\ [\\]:; | =,+ *<>] + ); 





如果你想使用正则表达式,请获取 Expresso [ ^ ] - 它是免费的,它会检查并生成正则表达式。


y有一些问题我们的字符串:



  • 你的角色类中的反斜杠不会被转义,所以 \ [被视为转义序列,但它不存在。如果你像 \\ 那样逃避反斜杠,那么它将最终成为一个反斜杠,正则表达式会将其解释为'下一个字符是意思是一个文字字符',所以你必须添加另一个反斜杠,并且,在你的字符串中,这看起来像另一个双反斜杠。因此,对于正则表达式中的字面反斜杠,您的字符串中需要4个反斜杠。

  • 字符中的] class结束了角色类,但是你希望它继续,所以你必须逃避] 。在正则表达式中,这将是 \] ,但因为你需要转义字符串中的反斜杠,它变为 \\]



因此它变为:

 std :: regex stringpattern(  [!/ \\\\ [\\]: ; | =,+ *<>] *); 



但我们尚未完成!此代码不会执行您想要的操作:如果字符串包含一个无效字符,则返回false。原因是regex_match匹配完整字符串,而您想要搜索。而不是 regex_match ,使用 regex_search



And最后一件事:字符类意味着零或更多之后的 * 。这不是你想要的:你想要'一个或多个'。您可以用*替换*但是因为您正在搜索,您也可以删除 * ,因此您的代码变为:

< pre lang =c ++> std :: regex stringpattern( [!/ \\\\ [ \\] :; | =,+ * LT;>]);

// ...

< span class =code-keyword> if
(std :: regex_search(s_input,stringpattern))
{
return < span class =code-keyword> false ;
}
返回 true ;


那是因为你忘了逃避字符组中的右方括号并且它们必须被双重转义(因此在字符串中放置一个反斜杠以供正则表达式解析器看到): br />

错误:[!/ \ []:; | =,+ *<>] *
正确:[!/ \\ \\\ [\\]:; | =,+ *<>] *



您也不需要将输入转换为ANSI当使用Visual C ++提供的 wregex 类时:

 bool ValidateLoginid(const CString& inputString)
{
std :: wregex stringpattern(L[!/ \\ [\\]:; | =,+ *<>] *);
return!std :: regex_match(inputString.GetString(),stringpattern);
}

另请注意,我已将参数更改为const引用,以避免传递值并指示它未更改。



如果您只想检查特定字符是否存在,也不需要使用正则表达式。然后只需使用 strstr CString :: FindOneOf


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

解决方案

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.


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:

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:

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

// ...

if (std::regex_search(s_input, stringpattern))
{
    return false;
}
return true;


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.


这篇关于用户输入的字符串验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆