Reputation: 2794
I need a function that returns all substrings between a regex expression that matches anything and a delimiter.
$str = "{random_one}[SUBSTRING1] blah blah blah {random_two}[SUBSTRING2] blah blah blah{random_one}[SUBSTRING3]";
$resultingArray = getSubstrings($str)
$resultingArray should result in:
array(
[0]: "SUBSTRING1",
[1]: "SUBSTRING2",
[2]: "SUBSTRING3"
)
I've been messing with regex with no luck. Any help would be greatly appreciated!
Upvotes: 1
Views: 30
Reputation: 9927
You can achieve that with this regular expression:
/{.+?}\[(.+?)\]/i
Details
{.+?} # anything between curly brackets, one or more times, ungreedily
\[ # a bracket, literally
(.+?) # anything one or more times, ungreedily. This is your capturing group - what you're after
\] # close bracket, literally
i # flag for case insensitivity
In PHP it would look like this:
<?php
$string = "{random_one}[SUBSTRING1] blah [SUBSTRINGX] blah blah {random_two}[SUBSTRING2] blah blah blah{random_one}[SUBSTRING3]";
preg_match_all("/{.+?}\[(.+?)\]/i", $string, $matches);
var_dump($matches[1]);
Upvotes: 2