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
本文介绍了一种使用Java正则表达式将文档中的特殊破折号(如em dash)统一替换为普通破折号的方法。这种方法可以避免因使用特殊字符导致系统无法识别而引发的异常。
7394

被折叠的 条评论
为什么被折叠?



