I am decent with regular expressions, but here is strong. Below is the problem with group 2 . However, I think this should be pretty easy for regular expression gurus ...
Problem
I am trying to match zero or more instances of a set of keywords, in any order
[Update: for future reference]
The simplest solution (derived from black panda ): ((keyword1 | keyword2 | keyword3 )*)
Note: the space after each word is significant!
In my case, this is translated into:
((static |final )*)
This is the easiest answer. A better, more efficient approach is in the black panda answer below. It allows you to use any number of spaces and faster to process the RE engine.
Enter
I need to break up the following input into very specific groups.
Note: numbers are not part of the input. That is, each line of input begins with the letter p.
- public static final int ONE = 1;
- public final static int TWO = 2;
- public final int THREE = 3;
- public static int FOUR = 4;
- private int FIVE = 5;
Group
I need to split the input into match groups so that
group 1 = public or private or protected
group 2 = 0 or more instances of "static" or "final" <- the group I'm struggling with
group 3 = data type
group 4 = variable name
group 5 = value
Group Information 2
Given the above data, group 2 will be as follows:
- static final
- final static
- the ultimate
- static
- [empty line]
Failed solutions
this is the regex I came up with and id is not working for group 2:
^.*(public|private|protected)\s+(static\s+|final\s+)*\s+([^ ]+)\s+([^ ]+)\s*(;|=)(.*)$
for group 2, I tried:
- (static \ s + | last \ S +) *
- (static | end) * \ s +
- (static | final) *
- (static \ | final \) *
Summary
What should be the regular expression for "group 2" that matches one or more instances of the words "static" or "final". The correct solution will be extensible to match any subset of any words, such as [static, finite, transitional, unstable].