question: private static String replaceOptionalVariable ( String s , Movie movie , boolean forFilename ) { Pattern regex = Pattern . compile ( "\\$.{1}" ) ; Matcher mat = regex . matcher ( s ) ; if ( mat . find ( ) ) { String rep = createDestination ( mat . group ( ) , movie , forFilename ) ; if ( rep . isEmpty ( ) ) { return "" ; } else { return s . replace ( mat . group ( ) , rep ) ; } } else { return "" ; } }
target:  replaces an optional variable, eg "{ Year $Y }"<br> if we have a year, "Year 2013" will be returned<br> if $Y replacement was empty, the complete optional tag will be empty.