2011-02-21 26 views
2

有人可以告訴我如何解決以下正則表達式?我試圖上傳的應用Appspot上,和我得到一個錯誤,我的名字不符合下列條件:幫助與Appspot正則表達式

^(?:[a-z\d\-]{1,100}\~)?(?:(?!\-)[a-z\d\-\.]{1,100}:)?(?!-)[a-z\d\-]{1,100}$ 
+1

哇。這是一個非常有趣的正則表達式,僅用於名稱字段。 – Spudley

回答

3

相當複雜,但我會試着煮規則降至英語。

  1. 名稱中只允許有一個代字號(〜),但它是可選的。如果有代字號,則它不能是名稱中的第一個字符,它必須在其前面至少有1個,最多100個字母,數字或破折號。

  2. 只有一個冒號(:)允許在名稱中,它是可選的。如果有一個冒號,它必須:

    一個。在代字符後出現,如果存在的話。

    b。在1個或多個字母,數字,破折號或句號之後出現。

    c。字母,數字,破折號和句號組不能以短劃線開頭。

  3. 名稱的其他部分可以包含任何字母,數字或破折號,高達一百個字符。可選的代字符和冒號部分必須位於它之前,並且不能以短劃線開頭。您必須使用至少一個字符,最多100

所允許的一些例子:其他:

  • 富〜bar.baz:垃圾郵件雞蛋

  • 東西 -

  • 由誰〜思想 - 這 - 是 - 一 - 好主意

  • together.we.stand:強

  • 簡宜工作

  • -starting劃線〜as.long.as.there.is.a:波浪線之後,它

不會被允許的一些例子:

  • -no-出發破折號

  • -no-starting-dash:帶冒號但不帶撇號

  • no.full.stops.without.a.colon.after。它

  • 〜無波浪線在最開始

  • :無結腸在最開始

  • 更〜比〜之一:波浪:或:結腸

+0

所以像abcdefg應該工作?我只是試圖輸入一個簡單的a-z唯一的名稱,沒有破折號,點,撇號等。我想知道它是否在他們的最後一個錯誤。 – idyllhands

+0

是的,這個正則表達式會匹配這樣一個簡單的名字,第三部分。 –

+0

謝謝!所以我猜這是Google的錯,這次不是我的=) – idyllhands

1
^ (anchor to start of string) 

    Any character in "a-z\d-" 
    At least 1, but not more than 100 times 
    ~ 

? (zero or one time) 

    zero-width negative lookahead 
    - 

    Any character in "a-z\d-." 
    At least 1, but not more than 100 times 
    : 

? (zero or one time) 
zero-width negative lookahead 
    - 

Any character in "a-z\d-" 
At least 1, but not more than 100 times 
$ (anchor to end of string) 
+0

所以需要代字號,破折號,冒號和第二個破折號? – idyllhands

+0

@idyllhands - 是的。似乎來自正則表達式。它像其中的任何一個都是必須的:) –

+1

不,這個代字號在名稱中是允許的,但是是可選的。如果它在那裏,它需要有1到100個字符之前(a-z,數字,短劃線)。 –

1
^     # start of string 
(?:    # Try to match the following: 
[a-z\d\-]{1,100} # - 1-100 of the characters a-z, 0-9 or - 
\~    # - followed by a ~ 
)?     # zero or one times. 
(?:    # Then try to match: 
(?!\-)   # - unless the first character is a - 
[a-z\d\-\.]{1,100}# - 1-100 of the characters a-z, 0-9, . or - 
:     # - followed by a : 
)?     # zero or one times. 
(?!-)    # Then (unless the next character is a -) match: 
[a-z\d\-]{1,100} # 1-100 of the characters a-z, 0-9 or - 
$     # until the end of the string.