Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to validate a String against many patterns

This is a question more about best practices/design patterns than regexps.

In short I have 3 values: from, to and the value I want to change. From has to match one of several patterns:

XX.X
>XX.X
>=XX.X
<XX.X
<=XX.X
XX.X-XX.X

Whereas To has to be a decimal number. Depending on what value is given in From I have to check whether a value I want to change satisfies the From condition. For example the user inputs "From: >100.00 To: 150.00" means that every value greater than 100.00 should be changed.

The regexp itself isn't a problem. The thing is if I match the whole From against one regexp and it passes I still need to check which option was inputted - this will generate at least 5 IFs in my code and every time I want to add another option I will need to add another IF - not cool. Same thing if I were to create 5 Patterns.

Now I have a HashMap which holds a pattern as the key and a ValueMatcher as the value. When a user inputs a From value then I match it in a loop against every key in that map and if it matches then I use the corresponding ValueMatcher to actually check if the value that I want to change satisfies the "From" value.

This aproach on the other hand requires me to have a HashMap with all the possibilities, a ValueMatcher interface and 5 implementations each with only 1 short "matches" methode. I think it sure is better than the IFs, but still looks like an exaggerated solution.

Is there any other way to do it? Or is this how I actually should do it? I really regret that we can't hold methods in a HashMap/pass them as arguments because then I'd only have 1 class with all the matching methodes and store them in a HashMap.

like image 975
Mateusz Dymczyk Avatar asked Aug 23 '11 14:08

Mateusz Dymczyk


People also ask

How do you validate a string?

To validate a string for alphabets you can either compare each character in the String with the characters in the English alphabet (both cases) or, use regular expressions.

How do you check if a string has a pattern?

Use re. match() to check if a string matches a pattern A pattern specifies a sequence of characters that can be matched to a string and follows the regular expression syntax in Python. re. match(pattern, string) tests if the regex pattern matches string .

How do you do multiple regex?

You can use alternation (|) operator to combine multiple patterns for your regexp. But in case you have various input and you will have to convert them to instance of Date from a string. Then you must follow in a sequence and validate the input one by one.


1 Answers

How about a chain of responsibility.

Each ValueMatcher object exactly one From/To rule and a reference to the next ValueMatcher in the chain. Each ValueMatcher has a method which examines a candidate and either transaforms it or passes it on to the next in the chain.

This way adding a new rule is a trivial extension and the controlling code just passes the candidate to the first member of the chain.

like image 155
djna Avatar answered Sep 23 '22 15:09

djna