Javaパターンの概念における以下のパターンの意味は何ですか?
^[\p{Alnum}]{6,7}$
6〜7文字の英数字にアクセスできることを理解しています。しかし、上記の形式の $ の意味は何ですか。
以下は、あなたが要求したフォーマットです (私はこのフォーマット「3AB 45D」が必要です。つまり、最初の 3 つの Alnum 文字とスペース、および 3 つの Alnum 文字です)。
^[\\p{Alnum}]{3}\\p{Space}[\\p{Alnum}]{3}$
あなたのパターン
^[\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)
アップデート:
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
}