简体   繁体   中英

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.

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 ?

Many thanks in advance, GK

Regex pattern

You could use a simple regex like this:

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

Working demo

在此处输入图片说明

Java code

For java you could use a code like below:

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 .

PS: Specify the regex to be caseless

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 .

It is unclear to me if you wish to match _dbf (lower case). If so simply change DBF to [Dd][Bb][Ff] :

_([Dd][Bb][Ff])|(REP)|(TABLE)

If you wish to match any more keywords just add another |(abc) group.

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.

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]$"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM