简体   繁体   English

换行符“\n\r”和数字的正则表达式

[英]Regex for newline "\n\r" and digits

I want to match \r\n1.1我要匹配\r\n1.1

blabla \r\n1.1 Entrepreneurship und Untern

I tried the following RegEx:我尝试了以下正则表达式:

"\r\n"\d\.\d\s+

What should it look like?它应该是什么样子?

Link: https://regex101.com/r/wwN9SR/1链接: https://regex101.com/r/wwN9SR/1

What should it look like?它应该是什么样子?

If your string contains carriage return and line feed characters如果您的字符串包含回车符和换行符

If, when you said in your post like bla \r\n1.1 , you mean this string has one carriage return and one line feed and 9 characters total, then you can:如果,当你在你的帖子中说bla \r\n1.1时,你的意思是这个字符串有一个回车符和一个换行符,总共有 9 个字符,那么你可以:

Regex.Match("blabla \r\n1.1 Entrepreneurship und Untern", "\r\n\\d\\.\\d\\s+");

(you don't need to \\r\\n because the regex engine will be OK with receiving CR LF as actual chars made by the compiler interpreting \r and \n, but the compiler won't appreciate you putting \d and \s in a non-verbatim string because these aren't known escape sequences the compiler can handle) (你不需要\\r\\n因为正则表达式引擎可以接收 CR LF 作为编译器解释 \r 和 \n 的实际字符,但编译器不会感谢你把 \d 和\s 在非逐字字符串中,因为这些不是编译器可以处理的已知转义序列)

Or或者

Regex.Match("blabla \r\n1.1 Entrepreneurship und Untern", @"\r\n\d\.\d\s+");

In this second form it's the Regex engine, not the compiler, that receives \r and interprets it as "carriage return".在第二种形式中,接收\r并将其解释为“回车”的是正则表达式引擎,而不是编译器。 Using @ turns off the compiler's process of converting any \r it sees to a carriage return character (char 13).使用 @ 会关闭编译器将它看到的任何\r转换为回车符(char 13)的过程。 This means it's the regex engine that sees \r and interprets it as "the user is looking for a carriage return character"这意味着看到\r并将其解释为“用户正在寻找回车符”的是正则表达式引擎


If your string contains slash, r, slash, n characters如果您的字符串包含斜杠,r,斜杠,n 个字符

If, when you said in your post like bla \r\n1.1 , you mean this string has literally a slash and an r, and a slash and an n for 11 characters total, then you can:如果,当你在你的帖子中说bla \r\n1.1时,你的意思是这个字符串实际上有一个斜杠和一个 r,以及一个斜杠和一个 n,总共 11 个字符,那么你可以:

Regex.Match(@"blabla \r\n1.1 Entrepreneurship und Untern", "\\\\r\\\\n\\d\\.\\d\\s+");

You need to put \\\\r because the compiler will turn this into \\r , and the Regex engine will then turn this into \r which is "literally slash followed by literally r".您需要输入\\\\r因为编译器会将其转换为\\r ,然后正则表达式引擎会将其转换为\r ,即“字面斜线后跟字面 r”。

If you just did \\\r , which the compiler would be happy with and turn into "backslash followed by carriage return", the regex engine would then be looking for a "backslash followed by carriage return" not "backslash followed by r"如果你只是做了\\\r ,编译器会很高兴并变成“反斜杠后跟回车”,那么正则表达式引擎将寻找“反斜杠后跟回车”而不是“反斜杠后跟 r”

If you just did \\r the compiler would turn this into "backslash followed by r` but the regex engine would then interpret this as "looking for a carriage return", when you're "looking for slash then r"如果您只是执行\\r ,编译器会将其转换为“反斜杠后跟 r”,但是当您“查找斜杠然后 r”时,正则表达式引擎会将其解释为“寻找回车符”

Or或者

Regex.Match("blabla \r\n1.1 Entrepreneurship und Untern",  @"\\r\\n\d\.\d\s");

For the same reasons: using an @ string stops the compiler transforming \\ to \ and \r to (a carriage return character) but you still have ta similar transformation carried out by the regex engine when it is interpreting.出于同样的原因:使用 @ 字符串会停止编译器将\\转换为\\r转换为(回车符),但是当正则表达式引擎进行解释时,您仍然可以执行类似的转换。 You need to make sure the regex engine sees \\r before it does its interpretation.您需要确保正则表达式引擎在解释之前看到\\r If it sees \r or (a carraige return) it will be looking for a carriage return when you are looking for "slash then r"如果它看到\r或(回车),它将在您寻找“斜线然后 r”时寻找回车


Final footnote: regexstorm.net uses the .NET regex engine and is often a better test site than non-.net regex engine based sites, when you're developing C# regexes最后脚注:当您开发 C# 正则表达式时,regexstorm.net 使用 .NET 正则表达式引擎,并且通常是比基于非.net 正则表达式引擎的站点更好的测试站点

In order to match为了匹配

  • \r\n1
  • \r\n2.1
  • \r\n13.1.2

You should escape the \ and the .您应该转义\. characters, because they are reserved characters regular expressions.字符,因为它们是保留字符正则表达式。 To escape them you add a \ character before them so your regex should be similar to:要转义它们,请在它们之前添加一个\字符,因此您的正则表达式应类似于:

\\r\\n(\d+\.?)+

When using that regex into a c# string literal, you will need to add a @ character before the start of the string to avoid the compiler interpreting the \ character (the \ character is reserved not only in the regex engine but also in the c# strings).当将该正则表达式用于 c# 字符串文字时,您需要在字符串的开头添加一个@字符以避免编译器解释\字符( \字符不仅在正则表达式引擎中保留,而且在 c# 字符串中保留)。

string myRegexExpression = @"\\r\\n(\d+\.?)+";

Explanation, because regular expressions are never easy to understand.解释,因为正则表达式从来都不容易理解。

  • \\r\\n matches the \r\n because we escaped the \ character \\r\\n匹配\r\n因为我们转义了\字符
  • \d+ matches one or more numbers \d+匹配一个或多个数字
  • \.? is used to match an optional .用于匹配可选的. character特点
  • the (\d+\.?)+ is used to match a repeating 1. sequence similar to 1 , 1.1 , 1.1.1 (\d+\.?)+用于匹配重复的1.序列,类似于1 , 1.1 , 1.1.1

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

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