java - 如何实现一个试图在 Java 中找到最相似匹配的正则表达式?
问题描述
给定字符串“abc xyz def”,我希望字符串“abc def”和“def abc”都能成功匹配,我如何用Java中的正则表达式来完成这个?
更多:最佳解决方案可能类似于 python 中的正则表达式模块提供的模糊搜索,以便为正则表达式提供额外的功能。
解决方案
您想要的在正则表达式中称为“组”。组放置在括号 () 中,并且可以是规定字符类型的文字或符号化正则表达式语法。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)*)*";
推荐阅读
- c# - 使用选项时应如何在 .NET Core 配置中键入可枚举属性?
- node.js - Deno 1.1.1 静态 HTTP 服务器
- visual-studio-code - 打开 Visual Studio 代码时出现黑屏
- .net - appsettings.json 文件中的 JSON 变量替换在 Azure DevOps 版本中不起作用
- mysql - Sequelize NM 关联无法识别包含模型
- asp.net-core - 如何发送 formdata 以及一些附加参数
- php - 如何将面包屑模式数据的编号从 1 替换为 ....?
- python - Python matplotlib 保存数字
- c - 退出循环后奇怪的多维数组值
- javascript - ReactJS 在键入字段并同时保存原始数据时自动格式化