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.
No comments:
Post a Comment