duandingyou3331 2019-07-26 17:30
浏览 36
已采纳

重复的任意捕获组

Given a string, eg.:

static_string.name__john.id__6.foo__bar.final_string

but with an arbitrary number of label__value. components, how can I repeat the capture groups, split them into label & value, and also capture the terminating final_string ?

For the above I'd want [name, john, id, 6, foo, bar, final_string]

Is something like this possible when I don't know the number of label__value. components in advance?

This is for golang / RE2 if that matters.

Update: I don't have the luxury of doing this in a few lines of code, and would need to do this in a single regex. The regex is defined in a config file to an application I don't control, so a code based loop with conditionals etc is unfortunately not possible.

  • 写回答

1条回答 默认 最新

  • douwei1128 2019-07-26 19:21
    关注

    This totally depends on what the thing you are putting this into expects.

    This is answer focused on getting you the capture groups in a basic way attempting to avoid any issues with the "thing" you are putting the regex into and RE2.

    Note: You might find that the final_string doesn't get the capture group index you expect with this method, but again depends on what you are putting the regex into.

    A regular expression that would match "one" and "no" key/value pairs the following is:

    ^[^.]+(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+))$
    
    • static_string.final_string
    • static_string.name__john.final_string

    To support one more key/value pair we repeat part of the regular expression: Part repeated:

    (?:\.([^.]+?)__([^.]+))?
    

    So to support 2 key value pairs the regular expression is:

    ^[^.]+(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+))$
    

    This now supports the following additional example:

    • static_string.name__john.foo__bar.final_string

    So if I expand that out to support 12 key value pairs the regular expression is:

    ^[^.]+(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+?)__([^.]+))?(?:\.([^.]+))$
    

    This supports the following additional examples:

    • static_string.name__john.id__6.foo__bar.final_string
    • static_string.name2_1b__john.id__6.foo__bar.final_string
    • static_string.name__john.id__6.foo__bar.name__john.id__6.foo__bar.name__john.id__6.foo__bar.name__john.id__6.foo__bar.final_string
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。