Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

regex - How to use J modifier in php?

From the php docs

J (PCRE_INFO_JCHANGED)
    The (?J) internal option setting changes the local PCRE_DUPNAMES option. 
    Allow duplicate names for subpatterns. 

So J modifer allows duplicate names in the named captruing groups. See the match information at the right side in this demo.

When i'm trying to check this modifier in php, it displays the below warning and it won't display any output.

PHP Warning:  preg_match_all(): Unknown modifier 'J'

Here is my code,

$str = "foobarbuzxxxxx";
preg_match_all('~(?<name>foo).*?(?<name>buz)~J', $str, $match);
print_r($match);

Then i able to solve this problem by adding the J modifier in the regex itself like,

'~(?J)(?<name>foo).*?(?<name>buz)~'

$str = "foobarbuzxxxxx";
preg_match_all('~(?J)(?<name>foo).*?(?<name>buz)~', $str, $match);
print_r($match);

Output:

Array
(
    [0] => Array
        (
            [0] => foobarbuz
        )

    [name] => Array
        (
            [0] => buz
        )

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

    [2] => Array
        (
            [0] => buz
        )

)

did you see that the name is an index for only the second group not the first group. But we define both under a single name. In this it clearly shows that there are two different groups with same name called name. But in php, print_r($match['name']); prints only the second group

Array
(
    [0] => buz
)

But not the first one. Why? I assumed that if we do print_r($match['name']);, it would display the both but not the first.

So my two questions regarding the use of J modifier in PHP are,

  1. Why it refer the second group instead of both? If it always refers to the last group then what's the need for duplicate names in the capturing groups?

  2. And also why adding the J modifier after the php delimiters displays warning (not working)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Because array could not contains two equal keys. That's why 'name' key comes first, but has the last value (it was reassigned).

  2. https://bugs.php.net/bug.php?id=47456

Currently you can only use PCRE_INFO_JCHANGED by (?J) in the pattern. I have suggested times ago to the maintainer, but it wasn't accept the addiction of 'J' as valid modifier in the extension.

Example of usage this modifier

preg_match_all('/(?J)(?<string>.*)1|(?<string>.*)2|(?<string>.*)3$/', "text2
pext3", $matches, PREG_SET_ORDER);
var_dump($matches);

preg_match_all('/(?J)(?<string>.*)1|(?<string>.*)2|(?<string>.*)3$/', "text2
pext3", $matches);
var_dump($matches);

http://3v4l.org/fnm76#v446


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...