1

I want to match:

  1. aaa
  2. aaa,bbb
  3. aaa,bbb,ccc

But not a list with a trailing comma. My current regex:

(\w{3},?)+

also matches lists with trailing commas (aaa,bbb,). I was thinking I could also do:

(\w{3})(,\w{3})*

but that is rather ugly. My real regex is not matching 3-letter-words, but something bigger, and repeating the regex is ugly. How can this be fixed?

  • Do you need tthe capturing group? – Panki Jun 13 at 8:00
  • Is it full line matching? – dedowsdi Jun 13 at 8:29
  • What you have is a decent solution to the problem. If you have a much bigger problem, state that – Inian Jun 13 at 8:31
  • Capturing group is not needed. Yes full line matching. And repeating the BIG regex twice does not sound decent ;) – Karlo Jun 13 at 8:47
  • 1
    I think a regex as simple as: [^,]$ answers the question as posted. If it doesn't what information would explain why it's not sufficient? – Hkoof Jun 13 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.