Regex php preg_match multiple occurences in string

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".

Answers


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];
    } 
}

Try this code below:

$pattern = "/[^By]+/";
$string = "findByHouseByStreetByPlain";
preg_match_all($pattern, $string, $matches);
var_dump($matches);

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] => 
        )

)

Need Your Help

oracle database connection from VS2010

c# asp.net oracle oracle11g

I am can successfully execute the following code on a particular web project on my local dev machine..

Get only issues that are linked with Jira REST-API

perl rest jira jql

Is there a way to get all issues from project a that are linked to any other issue with the Jira REST-API? I'm working with the Perl-module JIRA::REST.