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 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误