Sunday 11 August 2013

RegEx - Regular Expresions

I have been playing with RegEx in Java more and more, and it is fun working out how they work and even built some myself.

I needed RegEx to perform checks on strings to validate password and email strings.

Finding RegEx to check if a string has either capitals, lowercase or numbers was quite easy :-

Capital = ".*[A-Z].*";
Lowercase = ".*[a-z].*";
Number = ".*[0-9].*";

Finding Email validation RegEx was a bit harder as there are many different compositions, some being quite loose and others being way to precise.

After a lot of testing I settled on this one :-

Email = "\\b(?!,|/)(\\w+)(?!,|/)((-?\\w+)*)?(?!,|/)(\\.{1})?(\\w*)?@{1}(\\w*)(?!,|/)((-?)(\\w*)?)(?!,|/)(\\.{1}\\w+){1,2}\\b";

But finding a RegEx to find a symbol in a string was a bit harder still.

I read many many forums and tutorials and tried many many different compositions.

I even built an array of every RegEx I had built for each individual symbol using a break before each symbol to ensure no meta characters slipped by :-

@ Symbol = ".*[\\@].*"

Then I looped through he array and tested against my test stings.

Sure it worked, but what a lot of code for something that I though should have been quite straight forward.

Then after far too many hours I looked at this problem from a different angle, if I could not reliably test a string for a symbol why not test a string for a character that was not a capital, lowercase, whitespace, underscore or number.

So I built the following :-

 Regex = ".*[^A-Za-z0-9!@#$%^&*()_+=-{}~`].*";

In RegEx ^ = not, similar to Java ! = not.

This works but once again I am still defining each symbol.

So I kept working and testing until I built :-

BetterRegex = ".*[^A-Za-z0-9\s].*";

It took me far to long but wow I had some fun.

Now it is time to relax ... relax and eat chocolate.


Thursday 8 August 2013

Cabbage


Cabbage


Cabbage


Cabbage



RegEx - Regular Expresions

RegEx, (Regular Expresions), are truncated code strings that can be used to search other strings for patterns of characters.

RegEx are a lot of fun to play with.


http://en.wikipedia.org/wiki/Regular_expression

A great resource site that can help teach, test and share RegEx strings.

http://gskinner.com/RegExr/