简体   繁体   中英

Regex for newline "\n\r" and digits

I want to match \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

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:

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)

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". Using @ turns off the compiler's process of converting any \r it sees to a carriage return character (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"


If your string contains slash, r, slash, n characters

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:

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".

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"

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"

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. You need to make sure the regex engine sees \\r before it does its interpretation. If it sees \r or (a carraige return) it will be looking for a carriage return when you are looking for "slash then 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

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).

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
  • \d+ matches one or more numbers
  • \.? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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