简体   繁体   English

Java忽略字符串匹配中的特殊字符

[英]Java ignore special characters in string matching

I want to match two strings in java eg. 我想在java中匹配两个字符串,例如。

text: János 文字: János

searchExpression: Janos searchExpression: Janos

Since I don't want to replace all special characters, I thought I could just make the á a wildcard, so everything would match for this character. 因为我不想替换所有特殊字符,所以我认为我可以将á设为通配符,所以一切都符合这个角色。 For instance if I search in János with Jxnos , it should find it. 例如,如果我用Jxnos搜索János ,它应该找到它。 Of course there could be multiple special characters in the text. 当然,文本中可能有多个特殊字符。 Does anyone have an idea how I could achieve this via any pattern matcher, or do I have to compare char by char? 有没有人知道如何通过任何模式匹配器实现这一点,或者我必须通过char比较char?

use pattern and matcher classes with J\\\\Snos as regex. 使用模式和匹配器类与J\\\\Snos作为正则表达式。 \\\\S matches any non-space character. \\\\S匹配任何非空格字符。

String str = "foo János bar Jxnos";
Matcher m = Pattern.compile("J\\Snos").matcher(str);
while(m.find())
{
    System.out.println(m.group());
}

Output: 输出:

János
Jxnos

A possible solution would be to strip the accent with the help of Apache Commons StringUtils.stripAccents (input) method: 一个可能的解决方案是在Apache Commons StringUtils.stripAccents (输入)方法的帮助下去掉重音:

String input = StringUtils.stripAccents("János");
System.out.println(input); //Janos

Make sure to also read upon the more elaborate approaches based on the Normalizer class: Is there a way to get rid of accents and convert a whole string to regular letters? 确保还要阅读基于Normalizer类的更精细的方法: 有没有办法摆脱重音并将整个字符串转换为常规字母?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM