正则表达式不允许在 dot(.) 之后立即写入 dot(.)



我是QT和C 编程的初学者。我想在我的行编辑中使用正则表达式验证器,该验证器不允许在dot(。)之后立即编写dot(。)。这是我使用过的我的言论:

QRegExp reName("[a-zA-Z][a-zA-Z0-9. ]+ ")

但这还不足以完成我的任务。请有人帮我。

我正在寻找这样的东西 - 例如:

  • "营地。"。(接受)

  • " camp..new"(不接受)

  • " ca.mp.n.e.w"(接受)

怎么样:

^[a-zA-Z](?:.?[a-zA-Z0-9 ]+)+$

说明:

The regular expression:
^[a-zA-Z](?:.?[a-zA-Z0-9 ]+)+$
matches as follows:
NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
----------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    .?                      '.' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    [a-zA-Z0-9 ]+            any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9', ' ' (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )+                       end of grouping
----------------------------------------------------------------------

一般来说,您要做的就是说您有一个.,不跟随另一个.,否则一切都很好。从很大的棘手中,您需要的是负面的lookahead断言,但是请记住,.是一个重新摩擦,所以也会有一些后卫。

^(?:[^.]|.(?!.))*$

当然,您可能需要进一步调整。

以扩展形式:

^                  # Anchor at start
(?:                # Start sub-RE
   [^.]            #    Not a “.”
|                  #    or...
   . (?! . )     #    a “.” if not followed by a “.”
)*                 # As many of the sub-RE as necessary
$                  # Anchor at end

如果您无论如何都可以锚定东西,则可以简化一点:

(?:[^.]|.(?!.))*

最新更新