Eiffel regular expression validation
How do you create a regular expression for a certain string? And can you do it in the Assertion (precondition part of the code)?
I've been google-ing around but couldn't get anything convincing.
The question is like this:
Add a precondition to the DEPARTMENT (the class that we're working on) creation procedure that ensures t开发者_如何学Gohat the phone number is valid. There are three possible valid phone number formats. A valid phone number consists of one of:
- eight digits, the first of which is non-zero
- a leading zero, a single non-zero digit area code, and then eight digits, the first of which is non-zero
- a leading ‘+’, followed by a two digit country code, then a single non-zero digit area code, and then eight digits, the first of which is non-zero
Any embedded spaces are to be ignored when validating a phone number.
It is acceptable, but not required, to add a PHONE_NUMBER class to the system as part of solving this problem.
There are several different questions to be answered:
How to check if a given string matches a specified regular expression in Eiffel? One can use a class
RX_PCRE_MATCHER
from the Gobo library. The featurecompile
allows setting the required regular expression and the featurerecognizes
allows testing if the string matches it.How to write a regular expression for the given phone number specification? Something like
"(|0[1-9]|\+[0-9]{2}[1-9])[1-9][0-8]{7}"
should do though I have not checked it. It's possible to take intermediate white spaces into account in the regular expression itself, but it's much easier to get rid of them before passing to the regular expression matcher by applyingprune_all (' ')
on the input string.How to add a precondition to a creation procedure to verify that the argument satisfies it? Let's assume that from the previous items we constructed a function
is_phone_number
that takes aSTRING
and returns aBOOLEAN
that indicates if the specified string represents a valid phone number. A straightforward solution would be to writemake (tel: STRING) require is_phone_number (tel) ...
and have a feature
is_phone_number
in the classDEPARTMENT
itself. But this prevents us from checking if the specified string represents a phone number before calling this creation procedure. So it makes sense to moveis_phone_number
to the classPHONE_NUMBER_VALIDATOR
that classDEPARTMENT
will inherit. Similarly, ifPHONE_NUMBER
needs to validate the string against specified rules, it can inheritPHONE_NUMBER_VALIDATOR
and reuse the featureis_phone_number
.
Halikal actually worked this one out, but dudn't share until now ...
This works in eiffelStudio 6.2 (note - this is gobo)
http://se.inf.ethz.ch/old/people/leitner/gobo_guidelines/naming_conventions.html
A valid phone number consists of one of:
- eight digits, the first of which is non-zero
- a leading zero, a single non-zero digit area code, and then eight digits, the first of which is non-zero
- a leading + followed by a two digit country code, then a single non-zero digit area code, and then eight digits, the first of which is non-zero
Any embedded spaces are to be ignored when validating a phone number.
require -- 040 is ascii hex space
valid_phone:
match(phone, "^\040*[1-9]\040*([0-9]\040*){7}$") = TRUE or
match(phone, "^\040*0\040*([1-9]\040*){2}([0-9]\040*){7}$") = TRUE or
match(phone, "^\040*\+\040*([0-9]\040*){2}([1-9]\040*){2}([0-9]\040*){7}$") = TRUE
feature --Regular Expression check
match(text: STRING; pattern: STRING): BOOLEAN is
-- checks whether 'text' matches a regular expression 'pattern'
require
text /= Void
pattern /= Void
local
dfa: LX_DFA_REGULAR_EXPRESSION --There's the Trick!
do
create dfa.make
dfa.compile(pattern, True) --There's the Trick!
check -- regex must be compiled before we can use it
dfa.is_compiled;
end
Result := dfa.matches(text)
-- debug: make sure of which pattern
if dfa.matches (text) then
io.putstring(text + " matches " + pattern + "%N")
end
end
end
精彩评论