I’m trying to (in php)
- Verify that the contents of a textarea are valid
- Separate them into the tokens for processing
In order to be considered valid, it needs to be a string containing only numbers and letters that form a “code” that is anywhere from 3-6 in length, and the comma used to separate them. I’ve broken this down into something like this:
[A-Za-z0-9]{3,6},
I’m having trouble finishing it though. I want them to be able to separate with either a comma, or a space and comma space, comma space, etc. I only want there to be a comma if there is a following valid token.
For example, the input string:
abe 123, PlE43,54drt , r2344
Should be separated into the following tokens:
‘abe 123’ and ‘PlE43′ and ’54drt’ and ‘r2344’
How can I fix my regex to fit the conditions? (right now I’m having trouble making the comma optional, but if it is there I expect another valid token, as well as using comma and any combination of space before or after as a valid separator)
Assuming you want to collapse all spaces (i.e., both spaces inside a token and spaces between tokens/next to commas should be ignored), you can do it much more simply with some preprocessing.
This code will also report an error if you have two consecutive commas, or if you end the input string with a comma, because that would generate an empty element in
$tokenswhich does not validate by the 3-to-6 alphanumeric rule.See it in action.
Update: to preserve the spaces inside tokens, a slight modification would be required:
Be careful with this though, as it thinks that
is one valid token.