What are regular expression and how are they useful? [duplicate]
Possible Duplicate:
Useful Regular Expression Tutorial
Hello,
I recently started coding in javascript.
I came across "Regular Expressions" while I'm searching (goolgling) to validate forms (name, email ID's, etc.)
Can someone help this newbie coder by explaining:
- What are regular expression?
- How are they useful in programming?
- Are they simple to understand?
- Where can I get some good referenc开发者_Go百科e to learn these?
Any help is much appreciated.
Thank you!
1) what are regular expression
"A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is .*\.txt$.
"
2) how are they useful in programming
Very.
3) Are they simple to understand
It depends. They can range from simple to understand to nearly unintelligible.
4) where can I get some good reference to learn these
This is a very good place to start: http://www.regular-expressions.info/
1) and 2) are very easy to lookup... Have you tried Wikipedia? They are extremely useful and i find myself using them again and again.
3) Depends on the complexity of the regular expression itself and of your knowledge of regular expressions
4) Resources:
- http://www.regular-expressions.info/
- http://wiki.hypexr.org/wikka.php?wakka=Regex
- http://gnosis.cx/publish/programming/regular_expressions.html
- http://geekswithblogs.net/brcraju/articles/235.aspx
etc... just google it ;)
Happy coding
http://www.zytrax.com/tech/web/regex.htm,
http://www.webreference.com/js/column5/,
http://www.javascriptkit.com/javatutors/re.shtml
Best bet is to read up on them somewhere like Wikipedia and use an online tool to try them out. I've not been using regex that long but find that places like stackoverflow will come to your rescue if you get stuck and most of the time it's knowing if a regex is the correct thing to be using and then finding an example of what you are after and making it work for you.
Online regex testers:- http://www.regular-expressions.info/javascriptexample.html or http://www.fileformat.info/tool/regex.htm or google will show you loads more.
If you're on windows a good tool for regex is Expresso this has got built in examples to try out and it also shows you a breakdown of the regex so you can see what's it matching. Another nice feature of it that a colleague showed me recently is that it can generate source code (C# or VB) for the regex you enter - time saver!! Also may be worth buying a book on the subject I've heard that this book is good and there's loads of others that have good recommendations.
A regular expression is a textual representation of a finite state machine text parser with which you can at least:
- validate text
- "punch" text (fetch certain pre-defined slots)
- manipulate text
2) how are they useful in programming -
Any place where string matching or parsing is required.
Validate the format of an email or phone number input field
They are also useful in searching though logs to find specific lines (e.g. awk or gawk tools).
精彩评论