Regular expression: Difference between revisions
m (using an external editor) |
m (using an external editor) |
||
Line 12: | Line 12: | ||
* a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits | * a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits | ||
Regular expressions can be much more complex than these examples. | Regular expressions can be much more complex than these examples. ([http://en.wikipedia.org/wiki/Regular_expression Wikipedia], retrieved 16:41, 29 August 2008 (UTC)).}} | ||
Note: Regular expressions (although useful) are difficult to learn and usually only computer programmers use these. However, HTML and XML coders may consider learning some ... | Note: Regular expressions (although useful) are difficult to learn and usually only computer programmers use these. However, HTML and XML coders may consider learning some ... |
Revision as of 17:41, 29 August 2008
Definition
Regular expressions (regexps) provide a formalism to identify patterns in some text and any sort of other code. E.g. programmers when creating computer code use to find/replace text in some code, computer program scripts can use regexps to translate code from one form into another (e.g. HTML to Wiki), JavaScript programs may use regexps to check if user data entered in HTML form is correct, etc.
In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.
The following examples illustrate a few specifications that could be expressed in a regular expression:
- the sequence of characters "car" in any context, such as "car", "cartoon", or "bicarbonate"
- the word "car" when it appears as an isolated word
- the word "car" when preceded by the word "blue" or "red"
- a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits
Note: Regular expressions (although useful) are difficult to learn and usually only computer programmers use these. However, HTML and XML coders may consider learning some ...
An example
The following defines a somewhat legal Swiss Zip code:
CH-[0-9]{4,}
The following one defines a valid email address (example from http://www.regular-expressions.info/email.html)
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
Software
- Most good text editors provide regexp support
- Computer programming languages usually too (including scripting languages like PhP or JavaScript).
Links
- Overviews
- Regular expression (Wikipedia)
- regular-expressions.info Includes a catalogue of most popular ones)
- expreg.com
Programming languages
- In JavaScript
- Core_JavaScript_1.5_Guide:Regular_Expressions
- Introduction to Regular Expressions (microsoft)
- String Regular Expressions with JavaScript and ECMAScript
- Some explanations for a very simple chatter bot
Cheatsheets
- Regular Expressions Cheat Sheet by LoveJackDaniels.com / Dave Child.
Online tools
- regexpal.com (A testing tool for regexps)