Contact Us

Getting Started With Regular Expressions

Have you ever heard people talking about Regular Expressions before? Have you ever seen strange things like b[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}b. in other peoples code or web development projects and wondered what they are or how to construct them? Then you are in the right place!

In its most basic form, a Regular Expression is a way of filtering and matching text or numbers to fit our specific rules.

For example the following Regular Expression will match any string that starts with the word The:

^The

Syntax

Let’s get right into the syntax. Two of the most common characters in Regular Expressions are the Caret (^) and the Dollar Symbol ($). In effect, the Caret is placed before any piece of text that you want to match after it, as in the ^The example above, and the dollar symbol comes at the end of your matching text, so to match a string that begins and ends with the word The you would create the following regular expression:

^The$

The expression above would obviously only match a string that was only the word The, any other string would fail this match.

We also have the following 3 useful symbols, which are *, + and ?. These symbols determine how many times a character or sequence of characters may occur. They mean the following:

* – Zero or More
+ – One or More
? – Zero or One

Let’s use our The example from above and expand it slightly using our new selectors.

The* – This would match Th followed by any amount of e’s (Th, The, Thee, etc.)
The+ – This would match Th followed by at least one e (The, Thee, Theee etc.)
The? – This would match Th followed by no or one e (Th, The only)
T?he+ – This would match  T or no T followed by a h and at least one e (The, he, Thee, hee etc.)

Bounds

Bounds, which are open and closed curly braces {} are another useful part of Regular Expressions that can be used to indicate ranges in your Expressions. Let’s look at a few examples using our The example from above.

The{2} – This would match Th followed by exactly 2 e’s (Thee only)
The{2,} – This would match Th followed by at least 2 e’s (Thee, Theee, Theeee etc.)
The{3, 5} – This would match Th followed by 3 to 5 e’s (Theee, Theeee or Theeeee)

You must always specify the first number in a range, for example {,2} would not work, you would have to specify {0,2}. The symbols mentioned above *, + and ? are basically a quicker way of writing {0, 1}, {1,} and {0, 1}

This is just the basics of Regular Expressions and they are in themselves a massive subject, but well worth learning about. If you would like any help with your own web development needs then why not get in contact with us here at Network Intellect.

This article was posted by



Leave a Reply

What our clients say

"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
"They have always been professional and focused on delivering results in a timely and cost effective manner. It is enlightening to have an organisation that takes the time to make recommendations on actual data analysis."
Business Manager, Charity
"They (Network Intellect) continue to exhibit their digital expertise by helping us to establish a competitive advantage by pushing the boundaries and innovating their technology and performance solutions."
Head of Marketing Communications, Education
"Because of their talent, expertise and level of service our on-line business has increased dramatically."
Founder, Electronics
"I would have no hesitation in recommending them as an analytics partner to understand and improve the performance of a website."
Vice President, Retail
Network Intellect continue to give us a fantastic service, fast and friendly and are always ready to go the extra mile. They have gone above and beyond the call of duty to deliver our results.
Director, E-Cigarette Web
"We found that Network Intellect had the depth of knowledge we needed and after speaking to them a few times we decided to go with them. It’s the best decision we’ve made in some time."
Owner, Retail
"Their services offered great value for money. I would not hesitate to recommend them as a digital partner to others."
Strategic Marketing Manager, Education
"Jason and his team at Network Intellect embody efficiency and efficacy. I would have no hesitation in recommending this excellent company to others."
Marketing Manager, Education
Network Intellect have managed our product data feeds with exceptional care and meticulous detail. I know I can trust them to make the right decisions for our business."
Director, E-Commerce
"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
Read all testimonialsRead all testimonials

We can help you be great

We have friendly staff ready to help you get on your path to greatness...
Network Intellect Menu