Regular expression

A regular expression is a search pattern, its very often used in the programming language Perl, but it is used in other programming languages too or Text editors.

 

Meta characters

char meaning
^ defines the end of the matching String
$ defines the end of the matching String
. matches any character but not newline
* matches from 0 to infinity times
+ matches from 1 to infinity times
? matches 0 or 1 times
{} matches exact the given number or range
| logical or operator
() makes a group to store the result
[] makes a character matching group

 

Matches

\t tabulator
\n new line
\r return (CR)
\w matches from a-z,A-Z,0-9 and “_”.
\W matches nothing from a-z,A-Z,0-9 and “_”.
\s matches space, tab and newline
\S matches nothing from space, tab and newline
\d matches from 0-9
\D matches nothing from 0-9

 

Examples

This matches every string that starts with an “a”.

This pattern matches every string that end with an “a”.

This regex would match “schools” and “school”.

Would match every string with the length between 3 and 4.

This also would match “schools” and “school”.

On this “this is an ‘test’.” the pattern would store “test”.

This regex matches every combination from “a”,”c” and “o” like the word “coca”.

 

For testing Regex you could test the JavaScript Regex tester.

8 thoughts on “Regular expression”

Leave a Reply

Your email address will not be published.

three × 2 =