regex expression logic [closed]
Regex expressions seems cocumbersome, they always drive me mad when trying to build it up. And i end up asking for help. But once i am given the result it looks really coool. Gurus, is there a way to go about it to break it up.
Yuo can start with tutorials at Regular-Expressions.info which provide a good starting point. The reference there is also invaluable as are the explanations how the regex engine works behind the scenes.
Actually, the fundamentals aren't that hard to grasp and once you got them it's not too hard. However, reading complex regular expressions is more like an arcane art; it can get pretty unwieldy quickly.
Not sure what you mean by "break it up", but here are a couple regex tutorials:
The absolute bare minimum every programmer should know about regular expressions
Extreme regex foo: what you need to know to become a regular expression pro
Regulator is good, free tool that helps you to write regular expressions.
Doing web dev I have to do validation a lot. I usually have http://www.regexpal.com/ open in a tab somewhere. That site, combined with a tutorial, combined with a specific problem to solve is how I learned.
Learning regular expressions requires you to start thinking a bit differently. The syntax is not important, what's crucial is learning that a regex is a way to describe a pattern. Of course you have to consider that you're explaining it to someone (a machine) which is by itself quite dumb, so you have to be very thorough and not assume anything.
Example: you don't look for a word, you look for a sequence of characters which you know can be in a word. You know it, but the machine doesn't, so you have to be specific (well, there are shortcuts, but that's not the point).
So learn to think character-wise instead of the abstract concepts familiar in natural language, and above all practice a lot.
I won't add links as they are already mentioned in other answers. Good luck, regexp are fun!
Learn to think like the machine by reading How Regexes Work by Mark Dominus. For more, go with Mastering Regular Expressions by Jeffrey Friedl.
Several of the answers already posted provide very good RegEx resource material. However, one of the most important things to keep in mind before attempting to become the master of RegEx is that RegEx is not appropriate for all types of matching problem.
For example, most HTML cannot be parsed in a general sense using RegEx. I see tons of questions on SO where people are driving themselves nuts trying to apply RegEx in inappropriate situations - it drives me nuts to see it!
My advise is to first get a grasp of where RegEx is appropriate to use and where it is not. This alone will save you hours (days) of frustration.
The limitations of RegEx are those of using a finite state machine without a stack. The big thing to realize is that RegEx does not use a push-down stack to "remember" things it has come across. This is not a problem as long as the pattern matching process only needs to know where it is going next based on what it has matched so far and what it "sees" as incoming text. This strategy is adequate for matching very complex strings. However, it is not suited for situations where the matching process needs to:
- skip over chunks of input delimited by nested delimiters (as in XML, HTML and most programming language grammars)
- Matching some number of "things" to the same number of the same "things" when the "things" are separated by some other stuff. In otherwords, don't try to count the number of occurances of some pattern, skip some stuff, and then match a prior pattern. Sometimes it can be done - but expect a rough ride!
In other words, if you need to think recursively about matching anything, don't go there with RegEx. Recursion requires memory and RegEx does not remember very well!
Some implementations of RegEx have been augmented to deal with some limited types of recusions but you will have to get to "know" your version of RegEx to find out what the specific limitations (extentions) are.
This tool might help : http://www.gskinner.com/RegExr/
I could not completely understand your question but if you are looking for good and easier tutorial to get a tight fist on the regex then phpro.org has a great tutorial about it.
This is my favorite reg ex site regexlib
I recommend Expresso. Registration is needed after 30 days but it's completely free. It comes with a regex library and includes the author's 30 minute regex tutorial.
RegexBuddy is another popular tool but it's not free. You can view some demos here.
I suggest you go through the tutorial and other helpful sites mentioned so far. Start off with simple patterns and understand them, then move on from there. It's tricky at first, and you need to understand what to look for to design a pattern that matches it.
@gbacon mentioned the most popular book so I won't mention it again. A recent one that has good reviews is the Regular Expressions Cookbook.
精彩评论