Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of regular expression grouping

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of regular expression grouping. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Regular expression grouping:

If you want to match three numbers, the regular expression can be written as follows:

\ d {3}

The above code uses repetitive quantifiers to match 3 digits.

However, in practical applications, it is often necessary to repeat multiple characters. For example, if I want to repeat two characters of ab, the following code is not appropriate. The code is as follows:

Ab {3}

The above regular expression can only be repeated 3 times b.

In order to repeat multiple characters, you can use parentheses to specify subexpressions or groups to achieve this function, for example:

(ab) {3}

The above rule can repeat ab.

The so-called grouping is to use parentheses to include items into separate logical domains, so that you can deal with the contents of parentheses as if they were a separate unit.

Let's take a look at a code example:

(\ d {1pr 3}\.) {3}\ d {1pr 3}

Above is a simple ip matching regular expression. Content wrapped in parentheses is manipulated as a separate logical domain.

The role of grouping:

Grouping plays an important role in regular expressions, so here is a brief introduction to its functions.

one。 To serve as a primitive grouping:

Group individual items in order to synthesize subexpressions so that you can apply to them as if they were a single character. Wait for metacharacters to manipulate them.

The example code is as follows:

Var str= "I love javascript and java"; console.log (str.match (/ java (script)? / gi))

As can be seen from the running results of the above code, regular expressions can match both the string javascript and java, because grouping is used in regular expressions and repetitive quantifiers are used? You can make the previous subexpression repeat 0 or 1 times

two。 Define the submode:

Another very important role of grouping is to define subpatterns in a complete pattern.

When a regular expression successfully matches the target string, the part that matches the word expression in parentheses can be extracted from the target string.

If we match a string that begins with a number followed by one or more case-insensitive letters, the regular expression can be written as follows:

/\ d [a-zA-Z] + /

If what we really care about and need is the starting number, then we can put the numerical part of the regular expression in parentheses and extract the number from the retrieved match.

/ (\ d) [a-zA-Z] + /

three。 Reference subexpressions:

In regular expression syntax, previous subexpressions can be referenced after the same expression. This is achieved by following the number after the "\". This number specifies the position of the parenthesis expression in the regular expression, for example,\ 1 is the first parenthesized subexpression referenced, and naturally\ 2 is the second parenthesized subexpression.

Special note: because subexpressions can be nested with each other, you only need to determine the position of the left parenthesis when calculating the position of the subexpression. For example:

/ (java (script)) /

In the above regular expression, the nested sub-representation can be represented by\ 2.

A reference to a subexpression is not the matching pattern of the reference, but a reference to the matching content of the child. Generally speaking, references to subexpressions are generally used to enforce a constraint, as shown in the following code:

/ (['"]) [^'"] *\ 1 /

Usually, the left and right quotation marks match. If it is preceded by double quotes, the end must also be double quotation marks, and the front is single quotation marks, then the end must be single quotation marks, and no other quotation marks can appear in the middle of the string, for example:

"Welcome"

"Welcome"

Then the above regular expression implements this function. When the subexpression of the current face matches the single quotation mark, then the following\ 1 represents the single quotation mark, and if the child represents the matching double quotation mark, then the following\ 2 represents the double quotation mark.

Non-referenced grouping:

Grouping will take up some system resources, especially when the regular expression is long, it will slow down the matching speed. Sometimes just to set up a grouping and no references are needed, then grouping with non-reference types would be a good choice.

/ (java (? script)) /

The above rule is to use non-reference grouping, just add a?: after the left parenthesis.

This is the end of the article on "sample Analysis of regular expression grouping". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report