比方说,*必须跟着&.例如,
string asd = "Mother*&Mother*&Son";
// which is "Mother+ "*&" + "Mother" + "*&" + "Son"
// This is correct string.
Run Code Online (Sandbox Code Playgroud)
不好的例子,
string asd = "Mother*Mother*&Son";
string asf = "Mother**&Mother*&Son";
string asg = "Mother*&*Mother*&Son";
Run Code Online (Sandbox Code Playgroud)
如何在C#中检查字符串是否正确?
编辑
基于你们介绍的正则表达式的使用,我有一个附带问题.我实际上使用逗号(,)而不是星号(*)和引号(")代替&符号(&).在C#中,(让我使用其中一个人的例子)
Regex.IsMatch("Mother,\",Mother,\"Son", @"\,(?!")")
//won't work.. any idea?
Run Code Online (Sandbox Code Playgroud)
我也试过了
Regex.IsMatch("Mother,\",Mother,\"Son", @"\,(?!\")")
//not work, neither
Run Code Online (Sandbox Code Playgroud)
通过查找任何星号(*)后面没有&符号(&)查找失败:
Regex.IsMatch("Mother*&*Mother*&Son", @"\*(?!&)")
Run Code Online (Sandbox Code Playgroud)