简体   繁体   English

使用正则表达式检测“ \\”(反斜杠)

[英]Detecting “\” (backslash) using Regex

I have a C# Regex like 我有一个C#正则表达式

[\"\'\\/]+

that I want to use to evaluate and return error if certain special characters are found in a string. 如果在字符串中找到某些特殊字符,我想用它来评估并返回错误。

My test string is: 我的测试字符串是:

\test

I have a call to this method to validate the string: 我有一个对此方法的调用来验证字符串:

public static bool validateComments(string input, out string errorString)
{
    errorString = null;
    bool result;

    result = !Regex.IsMatch(input, "[\"\'\\/]+");  // result is true if no match
                                                   // return an error if match

    if (result == false)
        errorString = "Comments cannot contain quotes (double or single) or slashes.";

    return result;
}

However, I am unable to match the backslash. 但是,我无法匹配反斜杠。 I have tried several tools such as regexpal and a VS2012 extension that both seem to match this regex just fine, but the C# code itself won't. 我尝试了几种工具,例如regexpal和VS2012扩展,它们似乎都可以很好地匹配此regex,但是C#代码本身不行。 I do realize that C# is escaping the string as it is coming in from a Javascript Ajax call, so is there another way to match this string? 我确实意识到C#会将字符串从Javascript Ajax调用传入时转义,所以还有另一种匹配该字符串的方法吗?

It does match /test or 'test or "test, just not \\test 它确实匹配/ test或'test或“ test,但不匹配\\ test

The \\ is used even by Regex(es). \\甚至被正则表达式使用。 Try "[\\"\\'\\\\\\\\/]+" (so double escape the \\ ) 尝试"[\\"\\'\\\\\\\\/]+" (因此请对\\两次转义)

Note that you could have @"[""'\\\\/]+ " and perhaps it would be more readable :-) (by using the @ the only character you have to escape is the " , by the use of a second "" ) 请注意,您可能有@"[""'\\\\/]+ ”,也许它会更易读:-)(通过使用@ ,唯一必须转义的字符是" ,通过使用第二个""

You don't really need the + , because in the end [...] means "one of", and it's enough for you. 您实际上并不需要+ ,因为最后[...]意思是“其中之一”,对您来说就足够了。

Don't eat what you can't chew... Instead of regexes use 不要吃你无法咀嚼的东西...代替正则表达式使用

// result is true if no match
result = input.IndexOfAny(new[] { '"', '\'', '\\', '/' }) == -1;  

I don't think anyone ever lost the work because he preferred IndexOf instead of a regex :-) 我认为没有人会丢失工作,因为他更喜欢IndexOf而不是正则表达式:-)

您可以通过使字符串像这样@逐字地解决此问题:

result = !Regex.IsMatch(input, @"[\""\'\\/]+");

Since backslashes are used as escapes inside regex themselves, I find it best to use verbatim strings when working with the regex library: 由于反斜杠本身用作正则表达式内的转义符,因此我发现在使用正则表达式库时最好使用逐字字符串:

string input = @"\test";
bool result = !Regex.IsMatch(input, @"[""'\\]+");
//                                     ^^
// You need to double the double-quotes when working with verbatim strings;
// All other characters, including backslashes, remain unchanged.
if (!result) {
    Console.WriteLine("Comments cannot contain quotes (double or single) or slashes.");
}

The only issue with that is that you must double your double-quotes (which is ironically what you need to do in your case). 唯一的问题是您必须将双引号加倍(具有讽刺意味的是,这是您需要做的事情)。

Demo on ideone . ideone演示

For the trivial case, I am able to use regexhero.net for your test expression using the simple: 对于平凡的情况,我可以使用regexhero.net使用以下简单的测试表达式:

\\

to validate 验证

\test

The code generated by RegExHero: RegExHero生成的代码:

string strRegex = @"\\";

RegexOptions myRegexOptions = RegexOptions.IgnoreCase;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"\test";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM