开发者

regexp for find key value pairs separated by colon

开发者 https://www.devze.com 2023-01-30 22:25 出处:网络
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>

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号