I'm having trouble coming up with a regular expression for a string in the given form:
123123<key:value><key:value>,21313<key:val开发者_JAVA百科ue><key:value>
where the key:value pairs are optional, but we must not have two colons in the same key:value pairs.
I've gotten this far:
^((\d+)(<(.+?):(.+?)>)*)(,\d+)(<(.+?):(.+?)>)*$
some valid texts:
123131
123131, 123131, 1213313
12313<key:value>
232133<key:value><key:value>,232133<key:value><key:value>
Try this:
^((\d+)(<(.+?):(.+?)>){0,2})(,\s*((\d+)(<(.+?):(.+?)>){0,2}))*$
Depending on which group you don't want to capture, you can change ( )
to (?: )
.
Rubular link
Try using this ^(\d+(<.+:.+>){1,2})(,\d+(<.+:.+>){1,2})*$
Hope it helped
Thanks a lot for your responses, but none of them seem to do excactly what I'm looking for. I think maybe the easies thing is to follow OrangeDogs suggestion considering maintainability as well...
精彩评论