首页 > 解决方案 > 如何实现一个试图在 Java 中找到最相似匹配的正则表达式?

问题描述

给定字符串“abc xyz def”,我希望字符串“abc def”和“def abc”都能成功匹配,我如何用Java中的正则表达式来完成这个?

更多:最佳解决方案可能类似于 python 中的正则表达式模块提供的模糊搜索,以便为正则表达式提供额外的功能。

标签: javaregexfuzzy-search

解决方案


您想要的在正则表达式中称为“组”。组放置在括号 () 中,并且可以是规定字符类型的文字或符号化正则表达式语法。java.util.Scanner 和其他相关的 Iterator 接口和类或 java.lang.String 和用于正则表达式的主包 java.util.regex 包含用于组捕获的工具。

String abc = null; // weird but true for building regex into the expression on the fly in a re-callable method
String abc = new String("abc");
// hazard a guess such a data ***mining*** techniques would appear like this to capture groups whether present of not
Pattern p =  "((\b)*"+abc+"(\b)*)*   |   ((\b)*xyz(\b)*)* |  ((\b)*def(\b)*)*";

推荐阅读