开发者

Two or more matches in expression

开发者 https://www.devze.com 2023-01-18 04:35 出处:网络
Is it possible to make two matches of text - /12开发者_如何学Python3/123/123?edit I need to match 123, 123 ,123 and edit

Is it possible to make two matches of text - /12开发者_如何学Python3/123/123?edit

I need to match 123, 123 ,123 and edit

For the first(123,123,123): pattern is - ([^\/]+)

For the second(edit): pattern is - ([^\?=]*$)

Is it possible to match in one preg_match_all function, or I need to do it twice - one time for one pattern, second one for second?

Thanks !


You can do this with a single preg_match_all call:

$string = '/123/123/123?edit';
$matches = array();
preg_match_all('#(?<=[/?])\w+#', $string, $matches);

/* $matches will be:
Array
(
    [0] => Array
        (
            [0] => 123
            [1] => 123
            [2] => 123
            [3] => edit
        )

)
*/

See this in action at http://www.ideone.com/eb2dy

The pattern ((?<=[/?])\w+) uses a lookbehind to assert that either a slash or a question mark must precede a sequence of word characters (\w is a shorthand class equivalent to [a-z0-9_]).

0

精彩评论

暂无评论...
验证码 换一张
取 消