したがって、基本的には、 と の間のコンテンツを取得する必要が;あり:、:左側と;右側にあります。
この正規表現を使用できます: -
"(?<=:)(.*?)(?=;)"
これにはlook-behindfor:とlook-aheadforが含まれ;ます。And は、前に a がcolon(:)あり、後ろに a が続く文字列に一致しますsemi-colon (;)。
正規表現の説明: -
(?<= // Look behind assertion.
: // Check for preceding colon (:)
)
( // Capture group 1
. // Any character except newline
* // repeated 0 or more times
? // Reluctant matching. (Match shortest possible string)
)
(?= // Look ahead assertion
; // Check for string followed by `semi-colon (;)`
)
これが作業コードです: -
String str = "abc:def,ghi,jkl;mno:pqr,stu;vwx:yza,aaa,bbb;";
Matcher matcher = Pattern.compile("(?<=:)(.*?)(?=;)").matcher(str);
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
builder.append(matcher.group(1)).append(", ");
}
System.out.println(builder.substring(0, builder.lastIndexOf(",")));
出力: -
def,ghi,jkl, pqr,stu, yza,aaa,bbb