I have the following string:
findByHouseByStreetByPlain
How can I match to values after each "By". I have managed to find the first "By" value, but I can't get it going that it gives me all the matches for the value after "By".
Thsi regex should work for you:
<?php
$ptn = "#(?:By([A-Za-z]+?))(?=By|$)#";
$str = "findByByteByHouseNumber";
preg_match_all($ptn, $str, $matches, PREG_PATTERN_ORDER);
print_r($matches);
?>
this will be the output:
Array
(
[0] => Array
(
[0] => ByByte
[1] => ByHouseNumber
)
[1] => Array
(
[0] => Byte
[1] => HouseNumber
)
)
Some use of lookahead will do it
By(.*?)(?=By|$)
In php this become
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];
}
}
my string is different:
HouseByStreetByPlain
then i use the following regex:
<?php
$ptn = "/(?<=By|^)(?:.+?)(?=(By|$))/i";
$str = "HouseByStreetByPlain";
preg_match_all($ptn, $str, $matches);
print_r($matches);
?>
output:
Array
(
[0] => Array
(
[0] => House
[1] => Street
[2] => Plain
)
[1] => Array
(
[0] => By
[1] => By
[2] =>
)
)