[英]Regex not allowing backslash
I have a problem with my regex for not allowing backslash after the allowed characters. 我的正则表达式有一个问题,不允许在允许的字符后加反斜杠。 I have tested this through regex testers online and it is working, however when I use it on my c#, it returns true at all cost.
我已经通过正则表达式测试器在线测试了它,并且可以正常工作,但是当我在c#上使用它时,它不惜一切代价返回true。
This is my regex : var myRegex = new Regex("^[a-zA-Z0-9]+((([._-][^\\/<>()[\\\\]_.,;:\\\\s@\\"]+)*)|(\\".+\\"\\\\\\/))$");
这是我的正则表达式:
var myRegex = new Regex("^[a-zA-Z0-9]+((([._-][^\\/<>()[\\\\]_.,;:\\\\s@\\"]+)*)|(\\".+\\"\\\\\\/))$");
Input samples: 输入样本:
hello-\world : Negative
hello\world : Negative
hello/world : Negative
hello : Positive
However, this is the result when I implement it in c# 但是,这是我在c#中实现它的结果
Input samples: 输入样本:
hello-\\world : Positive (Which should be negative) hello- \\ world:正(应该为负)
hello\\world : Negative 你好\\世界:负数
hello/world : Negative 你好/世界:负面
hello : Positive 您好:肯定
I can't seem to find what's wrong with negating the backslash. 我似乎找不到反斜杠有什么问题。 Kinda hard to debug and check why the c# version of the Regex gives a different result.
Kinda很难调试并检查为什么Regex的c#版本会给出不同的结果。
Thanks for all the feedback. 感谢您的所有反馈。
EDIT: Updated the regex to c# 编辑:将正则表达式更新为C#
There was an error in your regex. 您的正则表达式中有一个错误。 Wrote some test to correct it:
编写了一些测试来纠正它:
var myRegex = new Regex("^[a-zA-Z0-9]+((([._-][^\\/<>()[\\]_.,;:\\s@\"\\\\]+)*)|(\".+\"\\\\/))$");
Assert.IsFalse(myRegex.IsMatch("hello-\\world"));
Assert.IsFalse(myRegex.IsMatch("hello\\world"));
Assert.IsFalse(myRegex.IsMatch("hello/world"));
Assert.IsTrue(myRegex.IsMatch("hello"));
Your first group allows dash, then didn't disallow backslashes. 您的第一组允许破折号,然后不允许反斜杠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.