Simple Regular Expressions
Basic patterns

Last updated on 14 Dec 2016 | Posted on 28 Nov 2016

If you haven't already, I'd recommend reading the regex reference I've previously written. It help give you the meaning of each character in a regular expression as well as ways that you can change the meaning of a character or what you wrap it, such as braces and brackets.

Start and Ends With

The character that indicates the 'start' of a string is ^ and $ indicates the 'end'.

^Expression

The above means that the string must start with the word "Expression".

Expression$

The above means that the string must end with the word "Expression".

You can then combine the two into something like this:

^Regular Expression$

This expression will only allow the words "Regular Expression".

To make it a little bit more dynamic you could change the above to:

^[a-z]$

This matches any characters from a to z (lower case), any number of times.

If we introduce capital letters and numbers as well, all we need to add is:

^[a-zA-Z0-9]$

If we introduce a dot (period) which matches any single character, we could enforce any 5 characters from start to end, like so:

^.{5}$

In this case, the number inside the braces refers to the number of required reptitions of any single character.

Brackets

As we've just seen, brackets can be used in expressions, and often are. To begin with:

[xy]

Matches either an 'x' or a 'y'.

[o-z]

Matches any letters between 'o' and 'z'.

^[a-fA-F]

Matches any letter (case insensitive) at the start of a string.

The '^' can also be used to negate something. The following means NOT an upper or lower case character between a and f or A and F:

[^a-fA-F]

Or NOT 0 to 9:

[^0-9]

 

Regex Reference

Regex Reference

Posted on 04 Feb 2016

This article is to simply define the special terms used in regular expressions.

Most of us need some kind of reference for regular expressions. Mainl…

Read more