我想在java中匹配两个字符串,例如。
text:János
searchExpression:Janos
由于我不想替换所有特殊字符,我认为我可以将á
设为通配符,因此所有内容都匹配此字符。例如,如果我使用János
在Jxnos
中搜索,则应该找到它。当然,文本中可能有多个特殊字符。有没有人知道如何通过任何模式匹配器实现这一点,或者我必须通过char比较char?
答案 0 :(得分:2)
使用带有J\\Snos
的模式和匹配器类作为正则表达式。 \\S
匹配任何非空格字符。
String str = "foo János bar Jxnos";
Matcher m = Pattern.compile("J\\Snos").matcher(str);
while(m.find())
{
System.out.println(m.group());
}
输出:
János
Jxnos
答案 1 :(得分:1)
一种可能的解决方案是在Apache Commons StringUtils.stripAccents(输入)方法的帮助下去除重音:
String input = StringUtils.stripAccents("János");
System.out.println(input); //Janos
请务必阅读基于Normalizer
课程的更精细的方法:Is there a way to get rid of accents and convert a whole string to regular letters?