Contact Us

Going Deeper With Regular Expressions

Following on from a previous post, titled Getting Started With Regular Expressions which you can view here we are now going to go a bit deeper into the world of our wonderful new friend, the Regular Expression.

In its most basic form, a Regular Expression is a way of filtering and matching text or numbers to fit very specific rules and is widely used in the world of Web Development. In our previous post we were using the simple word The to create our expressions, and here we will continue in the same vein.

Sequences

We will now look at matching character sequences in our text rather than single characters. For example, what if we wanted to match just the he part of the word The? Well, thankfully this is quite easy and involves the open and closed bracket symbols. Below is a couple of examples using this new technique, building upon what we’ve already learnt.

T(he)* This would match a string that has a T followed by zero or more copies of he (T, The, Thehe etc.)
T(he){1, 3} – This would match T followed by between 1 and 3 copies of he (The, Thehe, Thehehe only)

Or Operator

Using the pipe symbol ‘|’ we can also specify OR statements in our lines regular expression matches which enables us to say if this or that is true then a match is true, as shown in the examples below:

The|Th – This would match a string that has either The or Th anywhere inside it.
T(he|hee) – This would match a string with either The or Thee inside it.
T(he|hee)*n – This would match a string which has a T followed by a sequence of he’s or hee’s ending in an n. (Then, Theen, Thehen, Theeheen, Theehen, Theheen etc.)

. Period For Any Characters

The period symbol (‘.’) represents any character on the keyboard, for example you can specify specific string lengths or sequences of characters and numbers as shown below:

^.{3}$ – Only matches a string with exactly 3 characters
The.[0-9] – Matches The followed by a number (The0, The1, The2 etc.)

Square Brackets [ ]

Square brackets are used to specify allowed characters in a position of a string, below are a few examples to help clarify what is meant by this:

[ab] – Matches a string with either an a or a b (Same as a|b).
[a-d] – Matches a string that has the lowercase letters a to d (Sames as a|b|c|d or abcd).
^[a-zA-Z] – Matches a string that starts with any letter regardless of case.
[a-zA-Z0-9] – Matches a string that contains any letter or number.

You can negate your bracketed selection by placing a ^ symbol as the first symbol inside the bracket as follows:

[^a-zA-Z] – Matches a string that doesn’t begin with a letter.

The topic of regular expressions is very big and can become quite complex with longer queries and combinations of delimiters but the information included in this post and the previous one titled: Getting Started With Regular Expressions will give you a lot to get on with and should cover most basic situations needed throughout your sites.

If you need any help with your regular expressions or any area of web development 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