正确的正则表达式,用于用基本的“-"代替破折号在java中 [英] the correct regex for replacing em-dash with a basic "-" in java

查看:81
本文介绍了正确的正则表达式,用于用基本的“-"代替破折号在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与String类的replaceAll方法有关.

My question concerns the replaceAll method of String class.

我的目的是将文本中的所有破折号替换为基本的-".我知道em-dash的unicode字符是\ u2014.

My purpose is to replace all the em-dashes in a text with a basic "-". I know the unicode character of em-dash is \u2014.

我以以下方式尝试过:

String s = "asd – asd";
s = s.replaceAll("\u2014", "-");

仍然,不替换破折号.我在做什么错了?

Still, the em-dash is not replaced. What is it I'm doing wrong?

推荐答案

问题编辑后进行次要

您可能根本没有使用破折号.如果您不确定自己拥有什么,一个不错的解决方案是简单地找到并替换所有破折号... em或其他.看看 此答案 ,您可以尝试对所有的破折号使用 Unicode破折号标点属性 > \\ p {Pd}

You might not be using an em-dash at all. If you're not sure what you have, a nice solution is to simply find and replace all dashes... em or otherwise. Take a look at this answer, you can try to use the Unicode dash punctuation property for all dashes ==> \\p{Pd}

String s = "asd – asd";
s = s.replaceAll("\\p{Pd}", "-");


工作示例 用上述代码替换破折号和常规破折号.


Working example replacing an em dash and regular dash both with the above code.

参考文献:
Unicode正则表达式

References:
public String replaceAll(String regex, String replacement)
Unicode Regular Expressions

这篇关于正确的正则表达式,用于用基本的“-"代替破折号在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆