Reputation: 4574
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
Upvotes: 0
Views: 1856
Reputation: 30985
You could use a simple regex like this:
\b(\w+(?:_DBF|_REP|_TABLE|_TBL))\b
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`
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
Upvotes: 4
Reputation: 135
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
Upvotes: 0
Reputation: 2488
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:
Upvotes: 0
Reputation: 37023
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]$"));
Upvotes: 0