Reputation: 8100
I want to find the whole list of columns in the an SQL Select statement with Regex.
For example,
Select col1, col2,col3 from tab1;
Required
[1]: col1
[2]: col2
[3]: col3
One way is this regex
select.*from;
and then split the string. Is there any other more efficient method?
Thanks
Upvotes: 0
Views: 1891
Reputation: 336198
I'd say that your version is the most efficient way.
You could construct a regex that grabs all those in a single run, but it's surely not going to be as fast as what you're doing, and it only looks ahead to see a from
following, not behind whether there's a select
behind it:
Pattern regex = Pattern.compile("\\w+(?=\\s*(?:,|\\bfrom\\b))(?=.*\\bfrom\\b)");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
So all in all, I'd say go with what you have. Maybe make the asterisk lazy:
select.*?from
Upvotes: 1