douji8033 2018-02-28 15:57
浏览 43
已采纳

括号内用逗号标记

I'm desperately trying to tokenize a filter string which has following format:

price:gte:5.99,price:lte:10.00,model:in:[test1,test2]

The pitfall is that comma is used not only to separate filter options, but also as a separator for option lists: [test1,test2]

Applying standard regex tokenizer expression [^,]+ gives me:

price:gte:5.99
price:lte:10.00
model:in:[test1
test2]

But, obviously, I want:

price:gte:5.99
price:lte:10.00
model:in:[test1,test2]

How can I achieve that?

  • 写回答

2条回答 默认 最新

  • dongxichan8627 2018-02-28 16:31
    关注

    This might be an option:

    (?:[^][, ]+\[[^]]+\]|[^, ]+)

    Explanation

    (?:                  # Non capturing group
      [^][,
    ]+\[[^]]+\] # Match not a comma newline ] or [, then match between []
      |                  # Or
      [^,
    ]+            # Match not a comma or a newline
    )                    # Close non capturing group
    

    Edit: As suggested by @anubhava added ][ to the character class so that this becomes [^][, ] to prevent unnecessary backtracking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样