我想匹配两个字符串
我有预定义的词,如小麦、鸡蛋、面粉等.
我从wh3at,agg,f1Our等OCR上得到了.
因此,wh3at应该配上小麦或f1Our配上粉等。
发布于 2019-02-06 20:33:23
我曾经参与过OCR项目,在这些项目中,我们“规范化”了提取的文本。您可以构建与合理期望/观察到的输出相匹配的正则表达式。
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String[] strings = {"wh3at", "f1Our", "f10ur", "agg"};
for (String s : strings)
System.out.println(String.format("%s -> %s", s, normalizeWord(s)));
}
public static String normalizeWord(String unnormalized) {
if (Pattern.compile("(?i)wh(e|3)at").matcher(unnormalized).matches()) {
return "wheat";
} else if (Pattern.compile("(?i)f(1|L)(O|0)ur").matcher(unnormalized).matches()) {
return "flour";
} else if (Pattern.compile("(?i)(a|e)gg").matcher(unnormalized).matches()) {
return "egg";
}
return unnormalized;
}
}https://stackoverflow.com/questions/54561205
复制相似问题