What is the use of parentheses in regular expressions

In daily development, we rarely use parentheses in regular expressions, which have two functions.

The first function is grouping, that is to say, it can wrap a small piece of regularity and treat it as a whole. For example (\d{1,3}\.){3}\d{1,3}, in this regex (\d{1,3}.){3} means to match three digits plus a full stop, which is regarded as a whole repeated 3 times, and finally a one to three digits numbers (\d{1,3}). So you can regard the regex wrapped in parentheses as a new custom regex composed of existing regex.

The second function is to replace the square brackets. In actual use, we often use square brackets to match any one of the options, but in fact, you can replace the square brackets with parentheses.

Finally, when the parentheses function as a group, the multiple data matched by the group will be stored separately to form an array, and in some frameworks, they can be taken out and used separately. For example, we will encounter it when using nginx configuration.

Guess you like

Origin blog.csdn.net/dudadudadd/article/details/129828709