Hello, I'm new to the whole regex thing, and I'm having trouble understanding the syntax.

I have to separate a line by different criteria like spaces, brackets and others.

Here's and example line:

"$#*! My Dad Says" (2010) {Code Ed (#1.4)} 2010

What I want to have is:

$#*! My Dad Says
2010
Code Ed
#1.4
2010

This is what I have for now:

"#   \((.*?)\)  \{(.*?)\}   #"

<- should look for anything between () and {} if I'm not mistaken.

I'm not sure whether to use preg_split or preg_match as the first seems to remove my search pattern and the second displays 2 results: one with the delimiters and one without. Any help would be appreciated.

Member Avatar
diafol

I tried preg_replace:

$string = '"$#*! My Dad Says" (2010) {Code Ed (#1.4)} 2010';
$patterns = array();
$patterns[0] = '/^"/';
$patterns[1] = '/((" \()|(\) \{)|( \()|(\)\} ))/';

$replacements = array();
$replacements[0] = '';
$replacements[1] = "\n";
$str =  preg_replace($patterns, $replacements, $string);
echo nl2br($str);

Worked, but I'm sure there's an easier way.

Thanks it works. Would you ind explaining it to me (or maybe good me a good site about it)? I have to do other cases and I would rather learn how to to it right instead of coming here every time :)