Validate regular expression categories

Asked

Viewed 65 times

2

I would like to know how to validate categories with regular expressions. So I have a list of categories separated by comma. Each word must have a maximum of 20 characters, I need to validate every comma, if the word has alphanumeric characters.

I need to free up the spaces as I did below, but the category must have at least 1 character, but it cannot be made of space.

nothing , , look --- It can’t happen to stay space between commas.

I started doing so:

^[a-zA-Z0-9 ]{1,20}$

Car, bike, knife, nothing

Does anyone have any idea how I can do that.

1 answer

1

I performed tests here, and I believe this is the answer to my question:

^[\s]*[\w]+[\s]*(?:,[\s]*[\w]*[\s]*)*$

Even though she accepts 2 commas together, I let the user make a mistake and then get it right...

  • 1

    But in this example: Carro automático, moto 125, faca cega, your regular expression will ignore the whole line, is that really what you look for? If you could accept spaces, causing Carro automático be an item, you can use this expression: [^,\s][^\,]*[^,\s]*

  • 2

    So @Gerep , the idea is this, just 1 word per category. See you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.