Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams For capturing multiple groups use Regex.Matches it'll return a MatchCollection with all the matches. Metod Medja Feb 11, 2011 at 15:32

The regex (a) matches only a single a . In the first example above, that regex can be matched three times.

In the second example the regex matches several a s at once and captures each one into group 1.

To make a choice you should consider the following differences between them:

Regex.Matches("aaba", "(a)").Count // this is 3
Regex.Match("aaba", "(a)+").Groups[1].Captures.Count // this is 2

The second only yields two captures because it matches the first sequence of two as but then it stops matching when it finds a b. The + quantifier only matches unbroken sequences.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.