Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex matching 0 or more instances of key words, in any order
    text
    copied!<p>I'm decent with regexes but stumped here. I'm having trouble with <strong>group 2</strong>, below. However, I think this should be fairly easy for a regex guru...</p> <h2>Problem</h2> <p>I'm trying to match <code>zero or more instances of a set of keywords, in any order</code></p> <hr> <p><em>[Update: For future reference]<br> The simplest solution (derived from <a href="https://stackoverflow.com/users/261887/black-panda">black panda</a>'s response) is:</em><br> <code>((keyword1 | keyword2 | keyword3 )*)</code> </p> <p><em>note: the space after each word is essential!</em> </p> <p><em>In my case, this translated into:</em><br> <code>((static |final )*)</code></p> <p><em>this is is the bare-bones, simplest answer. The better, more performant approach is in <a href="https://stackoverflow.com/users/261887/black-panda">black panda</a>'s response, below. It allows for any amount of whitespace and is faster for a RE engine to process.</em></p> <hr> <h2>Input</h2> <p>I need to split the following input into very specific groups. </p> <p><em>Note: the numbers are not part of the input. That is, each input line starts with the letter p.</em></p> <ol> <li>public static final int ONE = 1; </li> <li>public final static int TWO = 2; </li> <li>public final int THREE = 3; </li> <li>public static int FOUR = 4; </li> <li>private int FIVE = 5;</li> </ol> <h2>Groups</h2> <p>I need to break the input into match groups such that</p> <p>group 1 = public or private or protected<br> <strong>group 2 = 0 or more instances of "static" or "final"</strong> &nbsp;&nbsp;&nbsp;&nbsp; <strong><em>&lt;-- group I'm struggling with</em></strong><br> group 3 = data type<br> group 4 = variable name<br> group 5 = value</p> <h2>Group 2 Details</h2> <p>Given the input above, group 2 would be as follows:</p> <ol> <li>static final</li> <li>final static</li> <li>final</li> <li>static</li> <li>[empty string]</li> </ol> <h2>Failed Solutions</h2> <p>this is the regex I came up with and id doesn't work for group 2:</p> <pre><code>^.*(public|private|protected)\s+(static\s+|final\s+)*\s+([^ ]+)\s+([^ ]+)\s*(;|=)(.*)$ </code></pre> <p>for group 2, I've tried:</p> <ul> <li>(static\s+|final\s+)*</li> <li>(static|final)*\s+</li> <li>(static |final )*</li> <li>(static\ |final\ )*</li> </ul> <h2>Summary</h2> <p>What should be the regular expression for "group 2" that matches one or more instances of the words "static" or "final". A proper solution would be expandable to match any subset of any words such as [static, final, transient, volatile].</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload