Elementary code example:

var text = "пришли не званы, уйдем не драны"; Regex regex = new Regex(@"[(зв)(др)]аны"); Match match = regex.Match(text); while (match.Success) { Console.WriteLine(match.Value); match = match.NextMatch(); } 

I expect to see "names" and "dran" in the conclusion, but for some reason there are only "vans" and "wounds". That is, it works as if there are no brackets. Why is it so - it is not clear, before that he already wrote regulars and everything was fine. Suddenly I ran into it when I didn’t get a more complex example and the analysis of errors led to this strange problem.

  • one
    and so (зв|др)аны ? - splash58
  • So it works out. But my example seems to work too. - Nikita
  • one
    because in square brackets everything is a single character and brackets lose their meaning. And the matches will also be "zana", "given", "(any", ") any". - KoVadim
  • 2
    round brackets lose their meaning - KoVadim
  • one
    @ splash58: Why not as an answer? - VladD

1 answer 1

Take a look at the online test of the regular expression [(зв)(др)]аны :

enter image description here

The fact is that a pair of unshielded square brackets defines a character class that finds only one character from the specified characters in the class. Inside such a class, most special regular expression metacharacters lose their special meaning, turning into a regular, literal character. For example, frequently used ( , ) , | , as well as quantifiers + ? , * and "anchors" ("bindings") ^ and $ inside the character class find the corresponding literal characters.

Always only need to escape the \ character, ^ needs to be escaped only at the beginning of the positive ("inclusive") character class (otherwise the "exclusive" character class will turn out), the ] character should be escaped only if it is not at the very beginning of the character class, but not screen at the beginning, end, and between the character range / predefined character set and another character.

You also need a grouping construct , so-called. (un) captured mask (or group), within which you can specify 1 or more alternative branches with the help of the operator | .

 (зв|др)аны (?:зв|др)аны 

See the regular expression demo

In the case of (зв|др) undercut is captured, i.e. if a match is found, you can look at match.Groups[1].Value , what was found, зв or др . On the contrary, if you use non-capturing traps (?:зв|др) , there will be no such possibility, match.Groups[1] will be null .

Briefly about all this you can read here and, of course, on Wikipedia, Regular Expressions .