2

So I have a text block something like below,

#start
Line 1
Line 2
Line 3
#end

*--Similar blocks-*

I used this

Regex.Matches(text,@"#start(.*?)#end",RegexOptions.Multiline)

regex for this above block.

The thing is when I try matching this Regex through Notepad++ find options it works, but through C# Regex.Matches function it fails.

I don't know what exactly is the problem. But hope you people can help me ! :)

kaustubhpatange
  • 173
  • 1
  • 10

2 Answers2

2

The dot character matches any character except newlines. Thus it will not match anything after the #start initial string. Try the following regex: #start(.|\s)+?#end (tested here).

As pointed out by other user's comments/answers, another simpler solution would be activating the "single line" regex option, which forces the regex matchers to consider the dot character as any character, including newlinew characters. This would allow OP's original regex to be used without modifications. Single line regex mode could be activated by passing the RegexOptions.Singleline option while matching the regex, as follows:

Regex.Matches(text, @"#start(.*?)#end", RegexOptions.Singleline);

vinicius.ras
  • 1,246
  • 2
  • 8
  • 18
  • 1
    another way to do it - just add a /s modifier at the end – Fattie Oct 15 '18 at 17:51
  • Thanks this works like a charm :) – kaustubhpatange Oct 15 '18 at 17:53
  • This is a very inefficient solution. Never use `(.|\s)+?`, it will make every regex slow and will decrease the code performance. Use `RegexOptions.Singleline` with a mere `.`. So, you may use `@"(?s)#start(.*?)#end"`, `@"#start(?s:.*?)#end"`, or `new Regex("#start(.*?)#end", RegexOptions.Singleline)` – Wiktor Stribiżew Oct 15 '18 at 19:32
  • I focused on explaining the reason why OP's original solution didn't work, not the performance side. I'm updating the answer to include these observations. – vinicius.ras Oct 15 '18 at 21:08
1

You need a Singleline regex mode. Try this code, it will print text between #start and #end

var input = @"#start
Line 1
Line 2
Line 3
#end

#start
Line 4
Line 5
Line 6
#end";

var reges = new Regex("#start(.*?)#end", RegexOptions.Singleline);
var blocks = reges.Matches(input).Cast<Match>();
foreach (var block in blocks)
    Console.WriteLine(block.Groups[1].Value);
Aleks Andreev
  • 6,732
  • 8
  • 27
  • 36