question: public static Collection < MessageFragmentStyle > colorizeRegex ( Style style , String text , Pattern regex , int group ) { ArrayList < MessageFragmentStyle > list = new ArrayList < > ( ) ; Matcher matcher = regex . matcher ( text ) ; while ( matcher . find ( ) ) { int start = matcher . start ( group ) ; int end = matcher . end ( group ) ; if ( end - start > 0 ) { MessageFragmentStyle messageFragmentStyle = new MessageFragmentStyle ( start , end - start , style , false ) ; list . add ( messageFragmentStyle ) ; } } return list ; }
target:  Apply styles on StyledDocument using regular expression