1

我有以下字符串:

findByHouseByStreetByPlain

如何匹配每个“By”之后的值。我设法找到了第一个“By”值,但我无法让它为我提供“By”之后值的所有匹配项。

4

4 回答 4

3

Thsi 正则表达式应该适合您:

<?php 
$ptn = "#(?:By([A-Za-z]+?))(?=By|$)#";
$str = "findByByteByHouseNumber";
preg_match_all($ptn, $str, $matches, PREG_PATTERN_ORDER);
print_r($matches);
?>

这将是输出:

Array
(
    [0] => Array
        (
            [0] => ByByte
            [1] => ByHouseNumber
        )

    [1] => Array
        (
            [0] => Byte
            [1] => HouseNumber
        )

)
于 2012-06-11T12:42:47.287 回答
1

一些前瞻的使用会做到这一点

By(.*?)(?=By|$)

在 php 这变成

preg_match_all('/By(.*?)(?=By|$)/', $subject, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
        # Matched text = $result[$matchi][$backrefi];
    } 
}
于 2012-06-11T12:32:45.830 回答
0

试试下面的代码:

$pattern = "/[^By]+/";
$string = "findByHouseByStreetByPlain";
preg_match_all($pattern, $string, $matches);
var_dump($matches);
于 2012-06-11T12:31:43.627 回答
0

我的字符串不同:

HouseByStreetByPlain

然后我使用以下正则表达式:

<?php 
$ptn = "/(?<=By|^)(?:.+?)(?=(By|$))/i";
$str = "HouseByStreetByPlain";
preg_match_all($ptn, $str, $matches);
print_r($matches);
?>

输出:

Array
(
    [0] => Array
        (
            [0] => House
            [1] => Street
            [2] => Plain
        )

    [1] => Array
        (
            [0] => By
            [1] => By
            [2] => 
        )

)
于 2015-10-16T14:46:03.613 回答