How, using regular expressions, to set the pattern of the form:

Начало ... конец ,

where ... these are any characters including spaces, underscores and integers, and how many are unknown. I'm trying to do it like this:

 var pattern = new Regex(@"Start:\S*\s*End"); 

because \S* - zero or more characters is not a space and \s* - zero or more characters are. However, this is wrong. How to fix ?

  • one
    ^Start:.*End$ - so try. . - any character. The quantifiers ^ and $ are the beginning and end of the string, respectively. - Andrew NOP
  • @ AndreiNOP I probably incorrectly described the problem. The fact is that the line is one and this pattern occurs several times in this line. Your option pernet 1 match - namely, the initial line. - Sergey
  • And what is the problem? Select parts enclosed between Start: and End ? Aah, you need a lazy quantifier :) Well, yes, the answer will suit you Wiktor Stribiżew - Andrey NOP

1 answer 1

Use

 var pattern = new Regex(@"Start:.*?End", RegexOptions.Singleline); 

Details

  • Start: - initial separator
  • .*? - 0 or more characters, as few as possible ( *? - lazy quantifier)
  • End - the final separator
  • RegexOptions.Singleline - flag, thanks to which . finds all characters, even line feeds.

Synim designs:

 var pattern = new Regex(@"(?s)Start:.*?End"); var pattern = new Regex(@"Start:(?s:.*?)End"); 

However, if you need to find a substring in which there should not be separators, you need to use the " tempered greedy token ":

 var pattern = new Regex(@"(?s)Start:(?:(?!Start:).)*?End"); ^^^^^^^^^^^^^^^^ 

This template in the Start: 1, Start: 2, End: 56 lines will find Start: 2, End , and not Start: 1, Start: 2, End .

  • Thank you, now that's right. I tried .* But it turned out that you just need .*? - Sergey