Reputation: 141
I need a regex for following values:
Ha2:123hD,Ha2:123hD,Ha2:123hD - correct match - true
Ha2:123hD,Ha2:123hD,Ha2:123hD, - comma on end - false
Ha2:123hD,Ha2:123hD,Ha2:123hD,,Ha2:123hD - double comma- false
,Ha2:123hD,Ha2:123hD,Ha2:123hD - comma at start- false
I am trying the following regex:
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+))/
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+,)*([a-zA-Z0-9]+)(\W)([a-zA-Z0-9])+$)/
But it is not working.
Upvotes: 1
Views: 771
Reputation: 29647
You could put the comma at the start of the repeating group.
/^[a-zA-Z0-9]+[:][a-zA-Z0-9]+(?:,[a-zA-Z0-9]+[:][a-zA-Z0-9]+)*$/
Upvotes: 1
Reputation: 626845
To only match comma-seaprated alphanumeric + :
string lists you may use
/^[a-zA-Z0-9:]+(?:,[a-zA-Z0-9:]+)*$/
See the regex demo
Explanation:
^
- start of string anchor[a-zA-Z0-9]+
- a character class matching 1 or more (due to +
quantifier)
ASCII letters or digits or :
(?:
- start of a non-capturing group....
,
- a comma[a-zA-Z0-9:]+
- a character class matching 1 or more (due to +
quantifier) ASCII letters or digits or :
)*
- .... 0 or more occurrences (due to the *
quantifier)$
- end of string.Upvotes: 0
Reputation: 5357
If you only need to check for these fix strings (As I have to guess from your question), this will work ^(Ha2:123hD,)*Ha2:123hD$
And otherwise, you just can follow this expression and replace the Ha2:123hD
with your wildcard expression.
Also check this website: https://regex101.com/ It explains nicely how the single symbols of a regex work.
Upvotes: 0