What's the difference between () and [] in regular expression patterns?

后端 未结 6 2040
梦谈多话
梦谈多话 2020-12-01 00:57

What is the difference between encasing part of a regular expression in () (parentheses) and doing it in [] (square brackets)?

How does this:

[a-z0         


        
6条回答
  •  无人及你
    2020-12-01 01:22

    (…) is a group that groups the contents like in math; (a-z0-9) is the grouped sequence of a-z0-9. Groups are particularly used with quantifiers that allow the preceding expression to be repeated as a whole: a*b* matches any number of a’s followed by any number of b’s, e.g. a, aaab, bbbbb, etc.; in contrast to that, (ab)* matches any number of ab’s, e.g. ab, abababab, etc.

    […] is a character class that describes the options for one single character; [a-z0-9] describes one single character that can be of the range az or 09.

提交回复
热议问题