存储未知字符的字符串

时间:2016-11-14 22:04:17

标签: java regex string character

让我说我有一个单词T y re。在某些国家/地区拼写不同 - T i 重新

是否可以在Java中存储字符串,例如 T y | i re ,如果我检查,也许使用regexp,它将返回true之类的< / p>

String compare = "Ty|ire";
String detail = "Tyre";
String detail2 = "Tire";
System.out.println(detail.equalsIgnoreCase(compare))  // prints "true"
System.out.println(detail2.equalsIgnoreCase(compare)) // also "true"

1 个答案:

答案 0 :(得分:6)

您正在寻找character class正则表达式String compare = "T[yi]re";[yi]构造与yi匹配。添加(?i)(嵌入式Pattern.CASE_INSENSITIVE选项)以不区分大小写的方式进行匹配:

String compare = "(?i)T[yi]re";
String detail = "Tyre";
String detail2 = "Tire";
System.out.println(detail.matches(compare));  // prints "true"
System.out.println(detail2.matches(compare)); // also "true"

请参阅online Java demo

如果您必须替换字符串的多字符部分,请使用交替组(y|ies)(?:y|ies)