Thursday, 7 April 2011

Working With Regular Expressions - RegExr Development Tool

Hey All,

So I am currently doing a Devlopment role, and during my day to day coding away I was asked to work with Regular Expressions.Now if you haven't heard of Regular Expressions (Commonly referred to as 'RegEx's') then let me fill you in:

RegEx's are used for pattern matching when searching text and/or strings in an application. They are very useful in various roles where you need to find certain words or group of words but don't want to manually code every possible combination.

For example:
Let's say that you have a website and you want to create a user 'blacklist', disallowing people from registering with any combination using the word 'admin'. Instead of coding each possible combination and then adding to them in a reactive manner, you could simply drop in a bit of code when a user registers, making sure it doesn't have a pattern that matches your Regular Expression.
This may look confusing in you've not worked with them before but the exact RegEx may look like this:

Now some of you may be looking at this and scratching your head, but it basically uses 'Expressions' in-lieu of characters, introducing 'fuzzy matching' to save us time. Using this RegEx, whenever a user enters a word such as Useradmin , or UserAdmin (detecting capitalisation) our bit of code will detect it and not allow the request.

Now you may ask, "What use is a bunch of character's to me, if I have no real clue what they represent?". Well this is where a tool like 'RegExr' comes in!

RegExr is an online tool which allows you to debug/test your regular expressions along with providing samples and explanations of what each character represents.

Click to Enlarge

The tool gives you a text field to dump a string/bunch of text into, and you can check which words your RegEx will anchor onto!

This method is a lot quicker than compiling a bunch of code, and is quite useful for the 'Begginer' or 'Rusty Expert' alike!

There is also a desktop version of the app available, which runs in Adobe Air to save on having to run it out of the browser.

Link: Site | Desktop

As always, feel free to comment, and if you have a question specifically about Regular Expressions, please comment here first as the answer could benefit others!


Post a Comment