Regex to match string between %

我是研究僧i 提交于 2019-12-07 16:33:08

问题


I'm trying to match substrings that are enclosed in %'s but preg_match_all seems to include several at the same time in the same line.

Code looks like this:

preg_match_all("/%.*%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches);
print_r($matches);

Which produces the following output.

Array
(
    [0] => Array
        (
            [0] => %hey%_thereyou're_a%rockstar%
            [1] => %there%
        )

)

However I'd like it to produce the following array instead:

[0] => %hey%
[1] => %rockstar%
[2] => %there%

What am I missing?


回答1:


Replace the "." in your regular expression with "[^%]":

preg_match_all("/%[^%]*%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches);

What is happening is that the "." is "greedily" matching as much as it possibly can, including everything up-to the final % on the line. Replacing it with the negated character class "[^%]" means that it will instead match anything except a percent, which will make it match just the bits that you want.

Another option would be to place a "?" after the dot, which tells it "don't be greedy":

preg_match_all("/%.*?%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches);

In the above example, either option will work, however there are times when you may be searching for something larger than a single character, so a negated character class will not help, so the solution is to un-greedify the match.




回答2:


You're doing a greedy match - use ? to make it ungreedy:

/%.*?%/

If a newline can occur inside the match, add the s (DOTALL) modifier:

/%.*?%/s



回答3:


Add a ? after the *:

preg_match_all("/%.*?%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches);



回答4:


The reason is that the star is greedy. That is, the star causes the regex engine to repeat the preceding token as often as possible. You should try .*? instead.




回答5:


You could try /%[^%]+%/ - this means in between the percent signs you only want to match characters which are not percent signs.

You could also maybe make the pattern ungreedy, e.g. /%.+%/U, so it will capture as little as possible (I think).




回答6:


|%(\w+)%| This will work exactly what do you want.



来源:https://stackoverflow.com/questions/1270682/regex-to-match-string-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!