2012-06-01 28 views
4

Java模式概念中以下模式的含義是什麼?Java模式不理解

^[\p{Alnum}]{6,7}$ 

我明白它可以訪問6至7個字母數字字符。但是以上格式的$是什麼意思。

+1

http://www.regular-expressions.info/anchors.html –

+0

感謝。我需要這種格式「3AB 45D」。這是第一次3 Alnum字符和空間和3 Alnum字符。我如何實現這一目標? – Srinivasan

回答

1

下面是你要求的格式(我需要這個格式 「3AB 45D」。即第一3個Alnum字符和空間和3個Alnum字符。),

^[\\p{Alnum}]{3}\\p{Space}[\\p{Alnum}]{3}$ 
+0

謝謝。這是工作。 – Srinivasan

2

$表示一行的結束,^表示行的開始。你提到的pattern被稱爲正則表達式

1

$意味着字符串的結尾。

如果你沒有在你的模式那麼它也將匹配6至7個字符的任何字母數字字符串....接着antyhing

我希望你得到^開始字符串的:)

0

你的模式

^[\p{Alnum}]{6,7}$ 

說明

"^" +    // Assert position at the beginning of the string 
"[\\p{Alnum}]" + // Match a single character present in the list below 
         // A character in the POSIX character class 「Alnum」 
    "{6,7}" +   // Between 6 and 7 times, as many times as possible, giving back as needed (greedy) 
"$"    // Assert position at the end of the string (or before the line break at the end of the string, if any) 

UPDATE:

try { 
    String resultString = subjectString.replaceAll("(?i)\\b(\\p{Alnum}{3})(\\p{Alnum}{3})\\b", "$1 $2"); 
} catch (PatternSyntaxException ex) { 
    // Syntax error in the regular expression 
} catch (IllegalArgumentException ex) { 
    // Syntax error in the replacement text (unescaped $ signs?) 
} catch (IndexOutOfBoundsException ex) { 
    // Non-existent backreference used the replacement text 
} 
+0

我需要這種格式「3AB 45D」。這是第一次3 Alnum字符和空間和3 Alnum字符。我如何實現這一目標? – Srinivasan

+0

@Srinivasan:看我的更新。 – Cylian