[英]Java Regex : How to return the whole word if the words ends with a specific string
Using Pattern/Matcher, I'm trying to find a regex in Java for searching in a text for table names that end with _DBF or _REP or _TABLE or _TBL and return the whole table names. 使用模式/匹配器,我试图在Java中找到一个正则表达式,以在文本中搜索以 _DBF或_REP或_TABLE或_TBL 结尾的表名,并返回整个表名。
These tables names may contain one or more underscores _ in between the table name. 这些表名可能在表名之间包含一个或多个下划线_。
For example I'd like to retrieve table names like : 例如,我想检索表名称,例如:
abc_def_DBF
fff_aaa_aaa_dbf
AAA_REP
123_frfg_244_gegw_TABLE
etc 等等
Could someone please propose a regex for this ? 有人可以为此提出正则表达式吗?
Or would it be easier to read text line by line and use String's method endsWith() instead ? 还是更容易逐行阅读文本并使用String的endsWith()方法代替?
Many thanks in advance, GK 预先感谢,GK
You could use a simple regex like this: 您可以使用一个简单的正则表达式,如下所示:
\b(\w+(?:_DBF|_REP|_TABLE|_TBL))\b
For java you could use a code like below: 对于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));
}
This is the match information: 这是比赛信息:
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`
If you aren't familiar with regex to understand how this pattern works the idea of this regex is: 如果您不熟悉正则表达式以了解此模式的工作原理,则此正则表达式的想法是:
\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
A simple alternative might be this regex ".*(_DBF|_REP|_TABLE|_TBL)$"
which means any string that ends in _DBF
or _REP
or _TABLE
or _TBL
. 一个简单的替代方法可能是此正则表达式
".*(_DBF|_REP|_TABLE|_TBL)$"
,表示以_DBF
或_REP
或_TABLE
或_TBL
结尾的任何字符串。
PS: Specify the regex to be caseless PS:将正则表达式指定为不区分大小写
This regexp should work to match the whole word: 此正则表达式应与整个单词匹配:
\w+_([Dd][Bb][Ff]|REP|TABLE)
Here is is: 这里是:
This regexp should work to match the keywords: 此正则表达式应与关键字匹配:
_(DBF)|(REP)|(TABLE)
The _
is matched, followed by either DBF
or REP
or TABLE
. 匹配
_
,后跟DBF
或REP
或TABLE
。
It is unclear to me if you wish to match _dbf
(lower case). 我不清楚您是否要匹配
_dbf
(小写)。 If so simply change DBF
to [Dd][Bb][Ff]
: 如果是这样,只需将
DBF
更改为[Dd][Bb][Ff]
:
_([Dd][Bb][Ff])|(REP)|(TABLE)
If you wish to match any more keywords just add another |(abc)
group. 如果您希望匹配更多的关键字,只需添加另一个
|(abc)
组。
Of course this method works only if you know that these "keywords" will appear only once, and only at the end of the string. 当然,仅当您知道这些“关键字”仅出现一次且仅出现在字符串的末尾时,此方法才有效。 If you have
123_frfg_TABLE_244_gegw_TABLE
for example you will match both. 例如,如果您有
123_frfg_TABLE_244_gegw_TABLE
,则将两者都匹配。
Below is a screenshot of regexpal in action: 下面是正则表达式的屏幕截图:
Try this: 尝试这个:
System.out.println("blah".matches(".*[_DBF|_REP|_TABLE|_TBL]$"));
System.out.println("blah_TBL".matches(".*[_DBF|_REP|_TABLE|_TBL]$"));
System.out.println("blah_TBL1".matches(".*[_DBF|_REP|_TABLE|_TBL]$"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.