开发者

Split by [char] but not by [char]{2} via regex

I need do it, basically:

Parameter    | Expected Result (array)
a            | [a]
a_b          | [a, b]
a_b_c        | [a, b, c]
a__b         | [a_b] <-- note that the double underline be unique
a_b__c       | [a, b_c]

I know how I do it with the methods explode and str_replace and using a foreach to converts __ to _. Basically this:

<?php

    $parameter = 'a_b__c';

    $expected = str_replace( '__', "\0", $parameter ); # "a_b\0c"
    $expected = explode( '_', $expected );             # ["a", "b\0c"]

    foreach ( $expected as &$item )
        $item = str_replace( "\0", '_', $ite开发者_StackOverflow中文版m );

    # ["a", "b_c"]

?>

But I guess that with preg_* it can be more fast. Or am I wrong?

Well, I accept any better suggestion. :)

Help note: the $parameter will be ever a PHP identifier (generally a class identifier).


You can try the following approach using preg_split:

$result = preg_split("/(?<!_)_(?!_)/", $parameter);

?<! and ?! are zero-width negative lookaround assertions.


I do not think it is possible to split and replace in a single regex operation. Let us say you have input a_b__c. In the first step you can split using the expression (same as what Howard gave)
$result = preg_split('/(?<!_)_(?!_)/', $subject); which would give you [a, b__c] Now you can iterate on each of the entry in the array and do the replace using
$result = preg_replace('/_(?=_)/', '', $subject); which would help you replace b__c with b_c.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜