Regular Expressions

From TekiWiki
Revision as of 21:15, 1 November 2016 by WikiSysop (Talk | contribs) (Other Links)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A Regular Expression is a compact way to express a text search or replacement.

Regular Expression Examples

About Regular Expressions

A Regular Expression is a compact way of expressing patterns to search for and, optionally, a replace. It started life within Unix grep, but has now been implemented in many different technologies, for example:

Languages: AWK, Perl, Java, JavaScript

Text Editors: TextPad, UltraEdit

Over the years, there have been extensions to the Regular Expression syntax, but there is a core which all implementations accept.

Search String Rules

^ - matches start of line

$ - matches end of line

. - matches any single character

[abc] - matches single a or b or c

[^abc] - matches a character other than a or b or c

(ab|cd) - matches ab or cd

* - matches zero or any number of the previous character/expression

+ - matches one or more of the previous character/expression

? - matches zero or one of the previous character/expression

{3} - matches three of the previous character/expression

{3,} - matches at least three of the previous character/expression

{3,5} - matches between three and five of the previous character/expression

\n - matches New Line character

\t - matches Tab character

\f - matches Form Feed character

\r - matches Carriage Return character

\v - matches Vertical Tab character

\d - matches any digit

\D - matches a character other than a digit

\w - matches any letter, digit or underscore (_)

\W - matches a character other than a letter, digit or underscore (_)

\s - matches any white space

\S - matches a character other than a white space

\b - matches a word boundary

\B - matches a character other than a word boundary

\. - matches an actual dot (works for all meta characters - including \)

Other characters match with themselves

Replacement String Rules

& - replace with entire matched string

$1 ... $99 - replace with first ... ninety-ninth matched bracketed () expression (provided there are that many in the match expression)

Other Links

Using Regular Expressions for NI Number Validation