the correct regex for replacing em-dash with a basic “-” in java
distinguish the different three dash punctuations '- – —' to transfer to basic '-'
- en dash(normal)
– macron
— em dash(\u2014)
if we cannot type the specific, a nice solution is to simply and replace all dashed with common regex --->\\p{Pd}
/**
* for jira ticket C167868-3225:
* when user use an rare char EM dash instead of general '-'
* the system cannot recognise and would throw an exception, so shall resolue
* this situation
* @description
* @date Apr 12, 2017
* @author wc62923
*/
private static String validateAndReplaceInvalidateChar(String subjectline){
LOGGER.info("start validating the subject if it is available and not contains any rare character...");
subjectline = subjectline.replaceAll("\\p{Pd}", "_");
LOGGER.info("end validating the subject, mail received with subject line " + subjectline + " before deattaching the documents...");
return subjectline;
}
public static void main(String[] args){
System.out.println("asd-–—asd".replaceAll("\\p{Pd}", "_"));
System.out.print("43AA9822-–—EM—TEST".replaceAll("\u2014", "_"));
}
the log:
asd___asd
43AA9822-–_EM_TEST