G
G
Governor2020-06-04 16:03:00
Regular Expressions
Governor, 2020-06-04 16:03:00

How to write an expression where (the last comma is optional)?

I want to check input tags. Tags consist of letters and numbers, separated by commas, there can be any number of spaces between tags.
The following expression almost copes: [ ]*([a-zA-Z0-9]+,[ ]*)*the only annoying thing is that you need to put a comma after the last tag. How can I make the last comma optional?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-06-04
@Mr-Governor

preg_match('/^ *[a-z\d]+(, *[a-z\d]+)*,? *$/i', $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question