Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for a list of items separated by comma or by comma and a space

Tags:

Hey, I can't figure out how to write a regular expression for my website, I would like to let the user input a list of items (tags) separated by comma or by comma and a space, for example "apple, pie,applepie". Would it be possible to have such regexp? Thanks!

EDIT: I would like a regexp for javascript in order to check the input before the user submits a form.

like image 924
Masiar Avatar asked Feb 15 '11 09:02

Masiar


People also ask

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

How do you put a space in a regular expression?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do you include a comma in regular expressions?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

What does \\ mean in regular expression?

You also need to use regex \\ to match "\" (back-slash). Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.


1 Answers

What you're looking for is deceptively easy:

[^,]+  

This will give you every comma-separated token, and will exclude empty tokens (if the user enters "a,,b" you will only get 'a' and 'b'), BUT it will break if they enter "a, ,b".

If you want to strip the spaces from either side properly (and exclude whitespace only elements), then it gets a tiny bit more complicated:

[^,\s][^\,]*[^,\s]* 

However, as has been mentioned in some of the comments, why do you need a regex where a simple split and trim will do the trick?

like image 141
Bennor McCarthy Avatar answered Sep 23 '22 06:09

Bennor McCarthy