如何过滤Java String以仅获取字母字符? [英] How to filter a Java String to get only alphabet characters?
问题描述
我正在生成一个XML文件来进行付款,我对用户的全名有一个约束。那个参数只接受字母字符(a-ZAZ)+空格来分隔名字和姓氏。
I'm generating a XML file to make payments and I have a constraint for user's full names. That param only accept alphabet characters (a-ZAZ) + whitespaces to separe names and surnames.
我无法以简单的方式过滤这个,我怎么能构建正则表达式或过滤器以获得我想要的输出?
I'm not able to filter this in a easy way, how can I build a regular expression or a filter to get my desireable output?
示例:
'CarmenLópez-Delina Santos'
必须'Carmen LopezDelina Santos'
我需要用单个元音转换带有装饰的元音,如下所示:á> a,à> a,a,等等;并删除点,连字符等特殊字符。
I need to transform vowels with decorations in single vowels as follows: á > a, à > a, â > a, and so on; and also remove special characters as dots, hyphens, etc.
谢谢!
推荐答案
您可以先使用规范化程序,然后删除不受欢迎的字符:
You can first use a Normalizer and then remove the undesired characters:
String input = "Carmen López-Delina Santos";
String withoutAccent = Normalizer.normalize(input, Normalizer.Form.NFD);
String output = withoutAccent.replaceAll("[^a-zA-Z ]", "");
System.out.println(output); //prints Carmen LopezDelina Santos
请注意,这可能不适用于所有和任何非ascii字母任何语言 - 如果遇到这种情况,该信件将被删除。一个这样的例子是土耳其语 i
。
Note that this may not work for all and any non-ascii letters in any language - if such a case is encountered the letter would be deleted. One such example is the Turkish i
.
在这种情况下的替代方案可能是列出所有可能的字母和他们的替代......
The alternative in that situation is probably to list all the possible letters and their replacement...
这篇关于如何过滤Java String以仅获取字母字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!