I want to match:

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

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


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


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?

  • 1
    Do you need tthe capturing group?
    – Panki
    Commented Jun 13, 2019 at 8:00
  • 1
    Is it full line matching?
    – dedowsdi
    Commented Jun 13, 2019 at 8:29
  • What you have is a decent solution to the problem. If you have a much bigger problem, state that
    – Inian
    Commented Jun 13, 2019 at 8:31
  • Capturing group is not needed. Yes full line matching. And repeating the BIG regex twice does not sound decent ;)
    – Karel
    Commented Jun 13, 2019 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
    Commented Jun 13, 2019 at 10:59

4 Answers 4


You can give a name to your big regex in PCRE like:


Everything after the ?<name> will be recorded with the name given.
Called Regular Expression Subroutines

So, repeats (?&name) become easy:


Test it Online

So, matching an IP, for example, becomes simpler:


Test it OnLine.

Use it with grep as:

grep -P '^(?<ip>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.(?&ip)){3}$'

If using perl or PCRE regexps, you can avoid repetition by using things like (?1) to refer to the regexp in the first capture group:

grep -Px '(?:(\w{3}),)*(?1)'

Which would match on any non-empty comma separated list of 3 character words. Change to:

grep -Px '(?:(?:(\w{3}),)*(?1))?'

To allow an empty list.


Please use - ^((([a-zA-Z0-9]){1,45},){2}([a-zA-Z0-9]){1,45})$

Here, I have set max word size to 45, as longest word in English is 45 characters, can be changed as per requirement


The regex as simple as: [^,]$ answers the question.

Thanks to @Hkoof for their comment!

  • 1
    It answers the question in that it matches on those 3 samples and not on anything that ends in , but it would also match anything that ends in a character other than , like aaaa or a,,,,b... What about your big regexp? Commented Dec 3, 2019 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .