有效的sharepoint文件夹名称的正则表达式

时间:2013-09-13 07:14:03

标签: regex

我正在尝试为有效的sharepoint文件夹名称制作正则表达式,其中包含条件:

  1. 无法以点开头或结尾,
  2. 不能包含连续点和
  3. 不能包含以下任何字符:〜“#%& *:<>?/ \ {|}。
  4. 写第一和第三点的正则表达式:

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

    和第三个(?!.*\.\.).*)$,但它们无法正常工作,必须将它们集成到一个表达式中。 请帮忙。

2 个答案:

答案 0 :(得分:1)

怎么样只是

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

我做了一个小测试here

修改

这也有效

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

答案 1 :(得分: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