Questions about regular expression capture

Source: Internet
Author: User
Question about regular expression capturing? $ Str = "a1234a"; $ preg = "/(\ d) {4}/I"; the above can match four consecutive numbers, in this case, the subexpression captures 1 (a subexpression )? Or 1324 (a subexpression )? Or 1, 2, 3, and 4 (four subexpressions )? Question about regular expression capturing?
$ Str = "a1234a ";
$ Preg = "/(\ d) {4}/I ";

The above can match four consecutive numbers,
Then the sub-expressions in the capture



Is 1 (a subexpression )?
Or 1324 (a subexpression )?
Or 1, 2, 3, and 4 (four subexpressions )? Share:
------ Solution --------------------
Let's take a look.
$str = "a1234a";
$preg = "/(\d){4}/i";
preg_match_all($preg, $str, $res);
print_r($res);
Array
(
[0] => Array
(
[0] = & gt; 1234
)

[1] => Array
(
[0] => 4
)

)

------ Solution --------------------
Reference:
$ Str = "a1234a ";
$ Preg = "/(\ d) {4}/I ";

The above can match four consecutive numbers,
Then the sub-expressions in the capture



Is 1 (a subexpression )?
Or 1324 (a subexpression )?
Or 1, 2, 3, and 4 (four subexpressions )?

Generally, the first value is the part obtained by the entire expression.
Then there is a bracket value.

Just like your regular expression
The first value is 1234
The second value is: 4.
If $ arr is used as an array
$ Arr [0] = 1234
$ Arr [1] = 4
------ Solution --------------------
The (\ d) {4} you used indicates that a single number character is a sub-group and repeated four times. Therefore, after execution, it should be
\ D Match 1 sub-group as 1
\ D matches 2 with the same sub-group and overwrites the previous value. The sub-group is 2.
\ D matches 3 with the same sub-group, overwrites the previous value, and the sub-group is 3
\ D matches 4 with the same sub-group and overwrites the previous value. The sub-group is 4.

If it is (\ d {4}), it is obviously different from the previous, meaning that four consecutive numeric characters are child groups.

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.