简体   繁体   English

Java Regex:如果单词以特定字符串结尾,如何返回整个单词

[英]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

Regex pattern 正则表达式模式

You could use a simple regex like this: 您可以使用一个简单的正则表达式,如下所示:

\b(\w+(?:_DBF|_REP|_TABLE|_TBL))\b

Working demo 工作演示

在此处输入图片说明

Java code Java代码

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`

Regex pattern explanation 正则表达式模式说明

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 . 匹配_ ,后跟DBFREPTABLE

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.

相关问题 在java中返回一串多个单词中的特定单词 - Return a specific word in a string of multiple words in java 如何将 Java 字符串一分为二,其中第一个子字符串不长于 x 并以整个单词结尾 - How to divide a Java String into two where the first substring is no longer than x and ends with a whole word Java-如何为特定单词解析字符串中的单词 - Java- how to parse for words in a string for a specific word 正则表达式通过开始子字符串替换单词并以 Java 中的点或空格结尾 - Regex to replace a word by starting sub String and ends with dot or space in Java (Java)将以特定单词开头和结尾的每个字符串存储到数组中 - (Java) storing every string that starts and ends with a specific word into an array Java正则表达式可获取以大写字母开头并以特定单词结尾的单词 - Java Regular Expression to get word or words that start with capital letter and ends with a specific word 如何在整个字符串中搜索特定单词? - How to search the whole string for a specific word? 正则表达式在java中查找字符串中的特定单词 - Regex to find a specific word in a string in java Java RegEx字符串,不包括特定单词 - Java RegEx string excluding specific word Java Regex:以's'结尾的单词(特定单词除外) - Java Regex: word ending in 's' except specific words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM