tags:

views:

172

answers:

6

Any recommendations, as I need to learn regular expressions to search for specific string and replace using specific operators. I have looked at the php functions such as preg_match && preg_replace and understood how they work but all I need is to be more familiar with the operators for expressions. Thanks.

+1  A: 

learn perl regular expressions as they are universal and you can use them in PHP as well.

dusoft
+13  A: 

PHP supports two sets/libraries, the first is Perl compatible regular expressions or PCRE, which are more modern and practical in use ( preg functions ) as opposed to the POSIX Extended Regular Expressions "standard" which is a tad outdated/ more bare-bones ( ereg functions ) compared to PCRE.

I would recommend going through the perlre tutorial, either use Perl or substitute the Perl methods with PHP ones. Of course Perl Regex engine is more advanced/up to date than the one in the PHP PCRE engine, but the core is pretty much the same.

If you really would like to purchase a book then I would recommend:

The first being more of a cookbook and is relatively new, the second is the definitive guide on regular expressions and covers them in various PCRE implementations and languages.

Good utilities for testing them live/online:

References:

meder
Do bear in mind that while PCRE is *similar* to Perl's regular expressions, and passes most of the Perl regex test suite, it's not the *same* :)
hobbs
The differences are mostly in the really obscure details though, so don't worry too hard.
hobbs
+2  A: 

regular-expressions.info is pretty good too, the quickstart guide is useful. Though can be a little complex

W3Schools is also a good place to look, has a nice list of operators.

andy-score
+3  A: 

http://www.regular-expressions.info that's where i started.... might want to use a regex testing tool too

Raz
great minds think alike
andy-score
If you like http://www.regular-expressions.info then you may like http://www.regexbuddy.com from the same author.
Jan Goyvaerts
+2  A: 

There is no such thing as PHP regular expressions =P There exists Perl-Compatible and POSIX Extended regular expressions in PHP.

A search on Amazon for regular expressions would list:

  1. Mastering Regular Expressions
  2. Regular Expressions Cookbook

in that order. Those books are the only books you´ll ever need in this subject.

anddoutoi
Though Amazon.com lists them in that order, I'd recommend starting with Regular Expressions Cookbook and finishing with Mastering Regular Expressions. REC has a lot more practical examples that will give you confidence, while MRE has a lot more technical details that you'll appreciate more when you're already familiar with regular expressions.
Jan Goyvaerts
+2  A: 

I'd recommend checking out this site

http://www.gskinner.com/RegExr/

I like it, because you can immediately see what your regular expression matches as you type.

Shoko