Reputation: 121
I need to specify strings of the form
A
A,A
A,A,A
etc.
using Pythonic regular expression (re
module). This example is simplified, A
will be another long regular expression.
I could do it using (A,)*A
but it seems to be redundant for me. The regular expression will be a part of some specification of a data format so it should be also human readable as much as possible. Is there any way how to write it shortly?
Expressions like
A,,A
A,A,
,A
should not be allowed.
Upvotes: 0
Views: 35
Reputation: 51430
Here's a solution:
^(?:A(?:,(?!$)|$))+$
Basically: match A
, then either match a ,
not at the end of the string ((?!$)
), or the end of the string.
Upvotes: 1