作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用模式/匹配器,我试图在Java中找到一个正则表达式,用于在文本中搜索以_DBF或_REP或_TABLE或_TBL结尾的表名称,并返回整个表名称。
这些表名称之间可以包含一个或多个下划线_。
例如,我想检索表名称,例如:
abc_def_DBF
fff_aaa_aaa_dbf
AAA_REP
123_frfg_244_gegw_TABLE
等等
有人可以为此提出一个正则表达式吗?
或者逐行读取文本并使用 String 的方法endsWith()会更容易吗?
提前非常感谢,门将
最佳答案
您可以使用像这样的简单正则表达式:
\b(\w+(?:_DBF|_REP|_TABLE|_TBL))\b
<强> Working demo
对于 Java,您可以使用如下代码:
String text = "HERE THE TEXT YOU WANT TO PARSE";
String patternStr = "\\b(\\w+(?:_DBF|_REP|_TABLE|_TBL))\\b";
Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
System.out.println("found: " + matcher.group(1));
}
这是比赛信息:
MATCH 1
1. [0-11] `abc_def_DBF`
MATCH 2
1. [28-43] `fff_aaa_aaa_dbf`
MATCH 3
1. [45-52] `AAA_REP`
MATCH 4
1. [54-77] `123_frfg_244_gegw_TABLE`
如果您不熟悉正则表达式以了解此模式的工作原理,则此正则表达式的想法是:
\b --> use word boundaries to avoid having anything like $%&abc
(\w+ --> table name can contain alphanumeric and underscore characters (\w is a shortcut for [A-Za-z_])
(?:_DBF|_REP|_TABLE|_TBL)) --> must finish with any of these combinations
\b --> word boundaries again
关于Java 正则表达式 : How to return the whole word if the words ends with a specific string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385271/
我是一名优秀的程序员,十分优秀!