There is a string: stringA chars1 stringB stringA chars2 stringB

Need to get:
1) stringA chars1 StringB
2) StringA chars2 StringB

If I set a pattern:

  string pattern = @ "stringA [sw] + stringB";

That results in the entire line: stringA chars1 stringB stringA chars2 stringB
How to make a template that takes all possible strings that do not have, for example, stringA? Or is there some other approach to solve this problem?

    1 answer 1

    This is called regular expression greed - it tries to choose the maximum string that matches the given pattern. Laziness - on the contrary, it will look for the smallest string.

    In C #, quantifiers are greedy and lazy.

    Greedy, for example: *, +; lazy, respectively: * ?, + ?. Using the lazy quantifier will just lead to the result that you want:

    string pattern = @"stringA[\s\w]+?stringB"; 

    It is written about this in more detail: Quantifiers .