• 0

Regex to match comma delimited numbers


Question

Hi there, i;m having a but of trouble coming up with a regex that matches a word followed by a list of numbers seperated by a comma

For example it should match word1,2,3 as well as word{1,2,3},4,5,{6,7,8} (implementing lists of numbers)

After a bit of reading and messing around i came up with the 'algorithm' i suppose you could call it of matching the word followed by either a list of numbers seperated by commas or a just a single number, then followed by a comma and then the same again

This is what i have so far

word({\d+(,\d+)*}|\d+,{\d+(,\d+)*}|\d+)*

This however only seems to match the word and nothing else

Is this i good way of going about this or should i rethink the design?

Any ideas appreciated!

Thanks again

4 answers to this question

Recommended Posts

  • 0

This is what I came up with. I used Expresso for this, you might find it helpful.

word\d*(({\d+(,\d+)*})|(\,\d*))*

EDIT: Nevermind, it matches a few things it shouldn't. I'll keep working on it...

EDIT 2: This regular expression seems to work properly:

word(\d+|{\d+(,\d+)*})(,(\d+|{\d+(,\d+)*}))*

The green shows the part of the expression that matches the first digit or list { }. Everything from then on must be separated by a comma.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.