protected static String negify ( String pattern ) { if ( pattern . contains ( ";" ) ) return pattern ; else { if ( pattern . contains ( "-" ) ) throw new IllegalStateException ( "Positive pattern contains negative sign" ) ; // the regex matches everything until the first non-quoted number character return pattern + ";" + pattern . replaceFirst ( "^([^#0,.']*('[^']*')?)*" , "$0-" ) ; } }
Guarantee a formatting pattern has a subpattern for negative values . This method takes a pattern that may be missing a negative subpattern and returns the same pattern with a negative subpattern appended as needed .