2

我正在尝试为有效的共享点文件夹名称制作正则表达式,它具有以下条件:

  1. 不能以点开头或结尾,
  2. 不能包含连续的点和
  3. 不能包含以下任何字符:~" # % & * : < > ? / \ { | }。

为第一点和第三点写了正则表达式:

[^\.]([^~ " # % & * : < > ? / \ { | }]+) [^\.]$

第三(?!.*\.\.).*)$,但它们不能正常工作,必须将它们整合到一个表达式中。请帮忙。

4

2 回答 2

1

刚刚呢

^\w(?:\w+\.?)*\w+$

我在这里做了一个小测试

编辑

这也有效

^\w(?:\w\.?)*\w+$
于 2013-09-13T10:23:09.733 回答
0

怎么样:

/^(?!^\.)(?!.*\.$)(?!.*\.\.)(?!.*[~"#%&*:<>?\/\\{|}]+).+$/ 

解释:

The regular expression:

(?-imsx:^(?!^\.)(?!.*\.$)(?!.*\.\.)(?!.*[~"#%&*:<>?/\\{|}]+).+$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    [~"#%&*:<>?/\\{|}]       any character of: '~', '"', '#', '%',
    +                        '&', '*', ':', '<', '>', '?', '/', '\\',
                             '{', '|', '}' (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

实际操作(perl 脚本):

my $re = qr/^(?!^\.)(?!.*\.$)(?!.*\.\.)(?!.*[~"#%&*:<>?\/\\{|}]+).+$/;
while(<DATA>) {
    chomp;
    say /$re/ ? "OK : $_" : "KO : $_";
}
__DATA__
.abc
abc.
a..b
abc

输出:

KO : .abc
KO : abc.
KO : a..b
OK : abc
于 2013-09-13T07:29:11.630 回答