We need subexpressions for complex and refined output. We can refine our output by grouping the expressions.
E.g. Test Case – HelloHello
RegEx – Hello{2,}
Result – Hello{2,}
It will not match any Hello because above expression is finding 'Hell' and atleast two occurence of 'o'. So, we need to create subexpression to find 'Hello' which itself is a expression. We use () for creating subexpression. Now the expression will be – (Hello){2,}
Nesting Subexpressions :
Subexpressions may be nested. In fact,subexpressions may be nested within subexpressions nested within subexpressions—you get the picture. The capability to nest subexpressions allows for incredibly powerful expressions,but it can also make patterns look convoluted,hard to read
and decode,and somewhat intimidating. The truth, however,is that nested subexpressions are seldom as complicated as they look.