I am very new to regex and just cannot figure out how to write a pattern to match what I need. Any help would be awesome!
I want to use PHP & regex to capture each set of characters in a string that follow a specific unique character (delimiter), plus any set of characters that precedes the first instance of that delimiter. I then want to "match" the desired output into a PHP array.
- Example delimiter:
>
Example string:
$str = 'word1 > word-2 > word.3 > word*4';
My desired match:
array([0] => 'word1', [1] => 'word-2', [2] => 'word.3', [3] => 'word*4',);
I've looked through the following responses, and while they are close, they don't quite help me achieve what I need:
- get all characters after "," character
- Regex to get the words after matching string
- Regex to get everything after the first space
This is the PHP function I'm currently working with, but it currently only finds the characters between the delimiter:
function parse_conditions($str, $delimiter='>') {
if (preg_match_all('/' . $delimiter . '(.*?)' . $delimiter . '/s', $str, $matches)) {
return $matches[1];
}
NOTE: the number of items in a given string may vary, so I can't use a pattern that expects a specific number of delimiters (ex. /^(.*?)>(.*?)>(.*?)>$/
)