Eiffel正则表达式验证
如何为某个字符串创建正则表达式?你能在断言(代码的前提部分)中做到这一点吗?
我一直在谷歌搜索,但找不到任何令人信服的东西。
问题是这样的:
向 DEPARTMENT(我们正在处理的类)创建过程添加一个前提条件,以确保电话号码有效。存在三种可能的有效电话号码格式。有效的电话号码由以下之一组成:
- 八位数字(第一位非零、
- 前导零)、单个非零位区号,然后是八位数字(第一位非零) 它是非零的
- 一个前导“+”,后跟两位数的国家/地区代码,然后是一个非零数字 区号,然后是八位数字,其中第一位非零
验证电话号码时,将忽略任何嵌入的空格。
可以接受但不是必需的,将 PHONE_NUMBER 类添加到系统中作为 解决这个问题。
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 that 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个不同的问题需要回答:
如何检查给定字符串是否与 Eiffel 中指定的正则表达式匹配?可以使用 Gobo 库中的
RX_PCRE_MATCHER
类。compile
功能允许设置所需的正则表达式,recognizes
功能允许测试字符串是否与其匹配。如何为给定的电话号码规范编写正则表达式?像
"(|0[1-9]|\+[0-9]{2}[1-9])[1-9][0-8]{7}"
应该尽管我还没有检查过。可以在正则表达式本身中考虑中间空格,但通过在输入字符串上应用prune_all (' ')
在传递到正则表达式匹配器之前删除它们要容易得多。如何向创建过程添加前提条件以验证参数是否满足?假设我们根据前面的项目构建了一个函数
is_phone_number
,它接受一个STRING
并返回一个BOOLEAN
,指示指定的字符串是否表示有效的字符串电话号码。一个简单的解决方案是编写并且在类
DEPARTMENT
本身中有一个功能is_phone_number
。但这阻止我们在调用此创建过程之前检查指定的字符串是否代表电话号码。因此,将is_phone_number
移动到DEPARTMENT
类将继承的PHONE_NUMBER_VALIDATOR
类是有意义的。 ,如果PHONE_NUMBER
需要根据指定规则验证字符串,它可以继承PHONE_NUMBER_VALIDATOR
并重用is_phone_number
功能。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 writeand 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 实际上解决了这个问题,但直到现在才分享...
这适用于 eiffelStudio 6.2(注意 - 这是 gobo)
http://se.inf.ethz.ch/old/people/leitner/gobo_guidelines/naming_conventions.html
有效的电话号码由以下之一组成:
然后是八位数字,其中第一位非零,
然后是一个非零数字的区号,然后是八位数字,
第一个非零
验证电话号码时,将忽略任何嵌入的空格。
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:
and then eight digits, the first of which is non-zero
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.