Eiffel正则表达式验证

发布于 2024-10-26 23:50:47 字数 394 浏览 3 评论 0原文

如何为某个字符串创建正则表达式?你能在断言(代码的前提部分)中做到这一点吗?

我一直在谷歌搜索,但找不到任何令人信服的东西。

问题是这样的:

向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2024-11-02 23:50:47

有几个不同的问题需要回答:

  1. 如何检查给定字符串是否与 Eiffel 中指定的正则表达式匹配?可以使用 Gobo 库中的 RX_PCRE_MATCHER 类。 compile 功能允许设置所需的正则表达式,recognizes 功能允许测试字符串是否与其匹配。

  2. 如何为给定的电话号码规范编写正则表达式?像 "(|0[1-9]|\+[0-9]{2}[1-9])[1-9][0-8]{7}" 应该尽管我还没有检查过。可以在正则表达式本身中考虑中间空格,但通过在输入字符串上应用 prune_all (' ') 在传递到正则表达式匹配器之前删除它们要容易得多。

  3. 如何向创建过程添加前提条件以验证参数是否满足?假设我们根据前面的项目构建了一个函数 is_phone_number,它接受一个 STRING 并返回一个 BOOLEAN,指示指定的字符串是否表示有效的字符串电话号码。一个简单的解决方案是编写

    make(电话:STRING)
        要求
            is_phone_number(电话)
        ...
    

    并且在类 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:

  1. 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 feature compile allows setting the required regular expression and the feature recognizes allows testing if the string matches it.

  2. 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 applying prune_all (' ') on the input string.

  3. 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 a STRING and returns a BOOLEAN that indicates if the specified string represents a valid phone number. A straightforward solution would be to write

    make (tel: STRING)
        require
            is_phone_number (tel)
        ...
    

    and have a feature is_phone_number in the class DEPARTMENT 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 move is_phone_number to the class PHONE_NUMBER_VALIDATOR that class DEPARTMENT will inherit. Similarly, if PHONE_NUMBER needs to validate the string against specified rules, it can inherit PHONE_NUMBER_VALIDATOR and reuse the feature is_phone_number.

野生奥特曼 2024-11-02 23:50:47

Halikal 实际上解决了这个问题,但直到现在才分享...

这适用于 eiffelStudio 6.2(注意 - 这是 gobo)

http://se.inf.ethz.ch/old/people/leitner/gobo_guidelines/naming_conventions.html

有效的电话号码由以下之一组成:

  • 八位数字,其中第一位非零,
  • 前导零,单个非零数字区号,
    然后是八位数字,其中第一位非零,
  • 前导 + 后跟两位国家/地区代码,
    然后是一个非零数字的区号,然后是八位数字,
    第一个非零

验证电话号码时,将忽略任何嵌入的空格。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文