dongmen9517 2018-04-04 20:26
浏览 117
已采纳

解析参数列表中用逗号分隔的逗号分隔值

So i have this regex:

=([0-9A-Za-z_-]+),?

and i need have a string like:

foo=bar,pine=apple,tree,bar=bie
or
foo=bar,pine=apple,tree
or 
pine=apple,tree

the regex works for cases where i only have 1 value. but since we have comma's in the list of values for the key. the regex just craps out and my code does half of what i want it to do but doesn't get the 2nd value.

How do i fix my regex to take both values regardless of where in the string it is? alone, between 2 others, at the end.

i tried some stuff but couldn't figure it out.

Attempt 1:

=([0-9A-Za-z,_-]+),=?

In this case, it matches the one where it's in the middle but it fails on the others because = does not exist.

Attempt 2:

=[0-9A-Za-z_-]+([,]+[0-9A-Za-z_-]*),?

Matches too bar,pine and tree,bar for example

EDIT::

This seems to work maybe....

=('[0-9A-Za-z,_-]+'),*|=([0-9A-Za-z_-]+),*

if i use quotes for multi values..

  • 写回答

1条回答 默认 最新

  • dsb0003795 2018-04-04 22:24
    关注

    You can split on variable names - that will leave only the values:

    s := regexp.MustCompile("[^,\\s]+=").Split("foo=bar,pine=apple,tree,bar=bie", -1)
    fmt.Println(s)
    # => [ "bar", "apple,tree", "bie"] 
    

    Go Demo

    Regex Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料