954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with regex

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.

Swiftle
Junior Poster in Training
51 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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 :)

Swiftle
Junior Poster in Training
51 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

My $0.02:

http://www.regular-expressions.info/

Am a happy user of RegExBuddy (paid).

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: