2

条件を持つ有効な共有ポイントフォルダー名の正規表現を作成しようとしています:

  1. ドットで開始または終了することはできません。
  2. 連続するドットを含めることはできません。
  3. 次の文字を含めることはできません: ~ " # % & * : < > ? / \ { | }。

1番目と3番目のポイントの正規表現を書きました:

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

そして 3 つ目(?!.*\.\.).*)$ですが、それらは適切に機能しておらず、1 つの式に統合する必要があります。助けてください。

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 に答える