title: Match a string with comma-separated sub strings, ensuring no leading/trailing commas, with regex in Kotlin
body: <p>There are many variations on this type of question, but none that I could see match exactly what I need - apologies if I've missed a different answer.</p>
  <p>As part of a unit test, I want to test whether a given string will match a regex pattern. The string will usually take a comma-separated form using the same set of substrings in the same order:</p>
  <p>accounts,leads,contacts,opportunities</p>
  <p>However, sometimes some of the substrings might not be present. For example, the string might be:</p>
  <p>accounts,contacts</p>
  <p>Or:</p>
  <p>leads,opportunities</p>
  <p>The string could also be empty.</p>
  <p>What I want is a regex pattern that will always match variations on this string and confirm that there are no trailing or leading commas around it. So far I have:</p>
  <pre><code>&quot;^[^,]?(accounts,?)?(leads,?)?(contacts,?)?(opportunities)?[^,]?$&quot;.toRegex()
  </code></pre>
  <p>But this doesn't work, as the regex will still accept one trailing space or comma, or letters other than commas around it. I'm not great at Regex, so any help is appreciated.</p>
  <p>Examples of what needs to match:</p>
  <pre><code>accounts,leads,contacts,opportunities // match
  &lt;emptystring&gt; // match
  accounts,opportunities // match, substrings are in correct order
  leads,opportunities // match, substrings are in correct order
  leads,accounts,contacts // no match, wrong order of substrings
  accounts,leads,contacts, // no match, trailing comma
  ,accounts,leads,contacts // no match, leading comma
  apples,bananas,contacts // no match, invalid substrings
  </code></pre>