简体   繁体   English

C# 正则表达式不匹配,但正则表达式测试人员匹配

[英]C# Regex doesn't match but regex testers do match

 var pattern = @"[\S\s][0-9]{1,}\e[0-9]{1,}";
 var regEx = new Regex(pattern);

This yields zero matches when run against this string:当针对此字符串运行时,这会产生零匹配:

"Show name - s01e08 - Episode name.mp4" “节目名称 - s01e08 - 剧集名称.mp4”

These regex testers get a match: http://www.regexr.com/ http://regexpal.com/这些正则表达式测试人员匹配: http : //www.regexr.com/ http://regexpal.com/

but this one that clams to be a .Net regex tester does not get a match: http://regexhero.net/tester/但是这个声称是 .Net 正则表达式测试器的人没有匹配: http : //regexhero.net/tester/

Which is what I am experiencing in my app.这就是我在我的应用程序中遇到的情况。

  1. Why is there a difference?为什么有区别?
  2. How do I 'fix' my regex pattern to get a match in my C# app?如何“修复”我的正则表达式模式以在我的 C# 应用程序中匹配?

Sidenote: The actual pattern I would like to use is:旁注:我想使用的实际模式是:

"[\\S\\s][0-9]{1,}[\\E\\e][0-9]{1,}" "[\\S\\s][0-9]{1,}[\\E\\e][0-9]{1,}"

But this gives me a 'Unrecognized escape sequence \\E.'但这给了我一个“无法识别的转义序列\\E”。 error in both the .Net regex tester and my C# app. .Net 正则表达式测试器和我的 C# 应用程序中的错误。 I will create a new SO question for this but wanted to mention it here as I'm sure someone will notice my original pattern will not catch upper case 'E'.我将为此创建一个新的 SO 问题,但想在这里提及它,因为我相信有人会注意到我的原始模式不会捕捉大写的“E”。

It looks like you are trying to write a RegEx for .NET without really using its syntax.看起来您正在尝试为 .NET 编写正则表达式,而没有真正使用其语法。 .NET does not use Posix syntax is what you might be using. .NET 不使用您可能正在使用的 Posix 语法。

Some mistakes that I can call:我可以称之为的一些错误:

  • When you want to match a letter, you should not espace it.当你想匹配一个字母时,你不应该对它进行空格。 You don't need any \\ before it.在它之前你不需要任何 \\ 。 To match an upper case E, just use E要匹配大写 E,只需使用 E
  • \\s matches whitespace in .NET. \\s 匹配 .NET 中的空格。 If you want to match a lowercase s, just write s如果你想匹配一个小写的s,就写s

Here is what your pattern means:这是您的模式的含义:

[\S\s] match one space character (\s) or one non-whitespace character (\S)
[0-9]{1,} match a digit (0 to 9) 1 times or more
\e match one escape character
[0-9]{1,} match a digit one or more times

Your pattern should be你的模式应该是

[sS](?<Season>\d+)[eE](?<Episode>\d+)

You basically do not have to escape characters inside range specifiers, and \\d+ is a more concise way of writing [0-9]{1,}您基本上不必对范围说明符内的字符进行转义,而\\d+是一种更简洁的书写方式[0-9]{1,}

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

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