
November 20th, 2012, 08:15 PM
|
 |
Lost in code
|
|
|
|
The regular-expressions.info site is really one of the best places to learn regexes. My recommendation is to just go to that site and basically read the entire tutorial section after reading the quick-start guide. The PHP manual entry is decent as well, but it isn't aimed as much as a beginner. It's a good reference, but not a great teacher.
Regular expressions are not simple and there's not really a good way to learn them short of memorizing what the special characters mean. This is the reason that most people will just spit out a string of characters when you ask them a regex question; it's a lot easier than trying to explain all the nuances of regular expressions. One example of that is this:
Quote: | The question mark in .*? means "be lazy": capture as few characters as necessary to meet the pattern. |
This is completely correct, in fact everything in AndrewSW's explanation is correct, however a question mark does not always mean 'be lazy'. It only means that in this case because it's following a *. In a situation like this: "(.*)?" it instead means 'be optional'. There are numerous characters that have multiple meanings like this depending on their context.
|