Why is it empty for $ matches [0] because regular expressions are crazy?

Source: Internet
Author: User
Tags html comment
Why is it empty for $ matches [0] because regular expressions are crazy? Write a regular expression, which can be matched normally, but I still need it to return the entire matched string.

The regular expression is as follows:
preg_match( '/^
 /', $_content, $matches);


$ _ Content is
 
 


I know that only the first one can be matched, but according to the official document: $ Matches [0] will contain the text matching the full mode
However, after a long test, I can only get the following results:
Array(    [0] =>     [1] => header)

$ Matches [0] why is it empty? What about the matched text? (I originally thought that the value of $ matches [0] is )

There is another problem. I don't know why the assertion won't match,
preg_match( '/^
 $/', $_content, $matches);

In this way, if a dollar sign is added at the end, nothing can be matched. Although this does not affect, I still want to know why and read the documents on the Internet, A bunch of terms are dizzy. I hope you can answer them. thank you.

Dizzy, how can I only give 100 points? I have 1000 points...


Reply to discussion (solution)

$_content =<<< HTML
 
 HTML;preg_match( '/^
 /', $_content, $matches);print_r($matches);
Array(    [0] => 
     [1] => header)
No problem

You are confused by yourself.
$ Matches [0] is an html tag that can be viewed only in text mode.

$_content='
 
 ';preg_match_all('/
 /', $_content, $matches);print_r($matches);/*Array(    [0] => Array        (            [0] => 
             [1] => 
         )    [1] => Array        (            [0] => header            [1] => footer        ))*/

$ Matches [0] will contain the text matching the full modeIt has the following sentence: $ Matches [1] will contain the text matched by the first capture sub-group, and so on.
This means that it only contains your anti-capture (that is, the content enclosed in your parentheses), rather than the global matching, so preg_match_all is used to perform global matching.
In addition, your regular expression is prefixed with ^, which indicates that the matching string must start with the content after ^. Yes.
Similarly, $ indicates that the matched string must end with $. therefore, to match the content, $ _ content =' '; Or $ _ content =' '; Can be matched

$_content =<<< HTML
 
 HTML;preg_match( '/^
 /', $_content, $matches);print_r($matches);
Array(    [0] => 
     [1] => header)
No problem

You are confused by yourself.
$ Matches [0] is an html tag that can be viewed only in text mode.



I got it !!! I forgot the HTML comment mark, dizzy, and really fainted for one night. thank you for reminding me.

$_content='
 
 ';preg_match_all('/
 /', $_content, $matches);print_r($matches);/*Array(    [0] => Array        (            [0] => 
             [1] => 
         )    [1] => Array        (            [0] => header            [1] => footer        ))*/

$ Matches [0] will contain the text matching the full modeIt has the following sentence: $ Matches [1] will contain the text matched by the first capture sub-group, and so on.
This means that it only contains your anti-capture (that is, the content enclosed in your parentheses), rather than the global matching, so preg_match_all is used to perform global matching.
In addition, your regular expression is prefixed with ^, which indicates that the matching string must start with the content after ^. Yes.
Similarly, $ indicates that the matched string must end with $. therefore, to match the content, $ _ content =' '; Or $ _ content =' '; Can be matched



I still don't quite understand. after ^ is used, can it be compared only from $ content [0? If the preceding strings are the same, match them. $ is the same?

There is another problem that we hope to solve.

A string exists.

$string = 'bbs/csdn/net/xxx';


If the string does not start with bbs, it will be matched.
For example
$string = 'www/csdn/net/xxx';

Because it does not start with bbs, continue matching

I think of a way to write
preg_match( '#((?!bbs/).)*#', 'www/csdn/net/xxx/', $matches);

However, I feel that the efficiency is a little low. I wonder if I can optimize it?

^ Is the starting position for matching the input string. if the string to be matched does not start with the content after ^, the matching will not be executed at all.

If it is just judgmentThe string does not start with bbs, This is faster

$ Str = '/bbs/www/csdn/net/xxx/'; if (strpos ($ str, 'bbs ')! = 0 | strpos ($ str, 'bbs ') = false) {// 0 indicates that the value starts with bbs. If no value is found, false is returned (all values are equal to false) echo 'exe ';}

^ Is the starting position for matching the input string. if the string to be matched does not start with the content after ^, the matching will not be executed at all.

If it is just judgmentThe string does not start with bbs, This is faster

$ Str = '/bbs/www/csdn/net/xxx/'; if (strpos ($ str, 'bbs ')! = 0 | strpos ($ str, 'bbs ') = false) {// 0 indicates that the value starts with bbs. If no value is found, false is returned (all values are equal to false) echo 'exe ';}



No. actually, I want to write a URL routing function similar to django. if the above method is used, the scalability is not strong enough, so I want to use regular expressions.

$ Str = 'www/bbs/net/xxx/'; if (preg_match ('/^ bbs. +? /', $ Str) {echo' starts with bbs ';} else {echo 'exe ';}

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.