In-depth understanding of regular expression syntax knowledge _ regular Expressions

Source: Internet
Author: User
Tags character set

4. Select and Group

(1). grouping

Character Group []: Indicates that one of several characters is matched

A group of characters can be easily understood as a combination of characters, the difference between a character group and a common character is that the ABC normal character represents a match a next B C and the character group [ABC] matches a or B or C in the same position. , because the character group itself also determines that the character group can be considered a normal special character.

Common: It is common because it also means that the operation of a single character at one position can also work on it, such as: [A-z]{6};

Particularity: Special because, 1. Other characters may have different meanings inside and outside of it, such as:-,.,^, 2. In the same position can have more than one choice, but this choice is limited to a character;

The differences between characters within and outside the character group:

-: Within a character group, a range is represented, and only one character is represented outside the character group;

Example:

-Outside the character group

<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
var reg =/16-8/g;
Console.log (Str.match (reg)); 16-8
</script>

-Within a character group

<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
reg =/1[6-8]/g;
Console.log (Str.match (reg)); 
</script>

-Outside the character group is a normal character that only represents-the character itself, so the match is 1 next is 6 next-Next is the 8 string; so it's 16-8.

-Within the character group is a range, so the match is 1 next 6 to 8 of any number of characters so is 16;

.: Represents a point within a character group, and any character outside of a group of characters (looking for a single character, in addition to a newline and a line terminator).

. Outside the character group

<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
var reg =/6.8/g;
Console.log (Str.match (reg)); 6-8
</script>

. Within a character group

<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
var reg =/6[.]8/ G;
Console.log (Str.match (reg)); Null
</script>

To represent any character outside the character group; so the match is 6. Next is any character followed by a string of 8, so the match is 6-8;

. In a character group it is only a common character that represents itself; so the match is 6. The next is a string of 8 that is NULL because it does not match;

For character groups, it can also be divided into matching character groups and excluded character groups mainly because of the change in the meaning of ^

^: off-character;

Example:

^ Outside the character set

<script type= "Text/javascript" >
var str = ' Han ';
var reg =/^ha/g;
Console.log (Str.match (reg)); Ha
</script>

^ Within a character set

<script type= "Text/javascript" >
var str = ' Han ';
var reg =/[^0-9]/g;
Console.log (Str.match (reg)); H,a,n
</script>

The caret outside [] is an anchor point that matches a character or string that starts with a certain character, in [] with the exception of the meaning

Note: [^a] represents a character that matches a position that is not equal to a, and does not understand that this position does not match a, and the latter's understanding can match a blank line, and notice the difference

Example:

<script type= "Text/javascript" >
var str = ' Han ';
var reg =/n[^q]/g;
Console.log (Str.match (reg)); Null
</script>

Note: This example does not have any characters after n, so the result of the match is null, and if you follow the second understanding above you may think that the result is: n

[] Regardless of the number of characters in the same location will only match to any one of them, if I want to match a string in a position that what?

(2). Select

At this point, you can use the () plus | This combination, | In the regular expression, or the meaning, and () dividing a range in a regular expression; a separate () use is meaningless, only if it is used in conjunction with other quantifiers to reflect its role (first of all, simple examples, greedy matches and non-greedy matches and then separate to say, This is not explained here first);

Example:

<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
var reg =/(16-8)/g;
Console.log (Str.match (reg)); 16-8
</script>
<script type= "Text/javascript" >
var str = ' 2016-8-20 ';
var reg =/16-8/g;
Console.log (Str.match (reg)); 16-8
</script>

This example has no parentheses that all represent the match 16-8 this string

When () and | Together, it is a multiple-selection structure that matches any subexpression () to represent a range; | represents a selection direction

Example:

<script type= "Text/javascript" >
var str = ' Greygray ';
var reg =/gr[ea]y/g;
Console.log (Str.match (reg)); Grey,gray
reg =/gr (e|a) y/g;
Console.log (Str.match (reg)); Grey,gray
</script>

The above two matching results are grey, gray means (|) of the branch selection structure to a certain extent, and [] can be substituted for each other

Example:

<script type= "Text/javascript" >
var str = ' Greygray ';
var reg =/gre|ay|yg/g;
Console.log (Str.match (reg)); Gre,yg,ay
</script>

Output Result: Gre,yg,ay

The addition or absence of parentheses has a significant effect on the result:

^hello | World | Hi: The match is ^hello or world or hi

^ (Hello | world | hi):. The matching start line is ^hello or ^world or ^ hi

Note: Although multiple-selection structures can sometimes be expressed as Fu Zulai, they can be represented to each other, but a character group can match only a single character in the target text, and each multiple-selection structure itself may be a complete regular, which matches any length of text

Some points to note in regular expressions:

1. In regular expressions, the space character is also treated as a rule character;

The meanings of 2.-;.;* are not the same as those in [] or outside of []. Notice the difference between the metacharacters and the characters in the group;

3. When adding [] to a few characters, the string is treated as a character group, and a single character is manipulated. * can also be used for its use, the mixture between quantifiers and quantifiers and packet selection between the mix will have a greedy and non-greedy matching concept, this will be a separate summary;

4. The difference between the branching structure and the character group; a branch can match any length of text; a character group can match only one character;

5. Note the understanding of some concepts: for example: [^x] means ' match a character not equal to X ' instead of ' only if this position is not x to match '; [^x] The concept behind can match a blank line in the example {x,y},? , * the understanding;

6. Determining whether a match is successful in a match is that the x,x in {x,y} plays a key role in matching success.

The above is a small set to bring you in-depth understanding of the regular expression of the grammar knowledge of all the narrative, hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, and then this page is very grateful to the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.