douqiangchuai7674 2019-02-27 10:17
浏览 128

否定Golang正则表达式中的整个字符串

I want to write a regex where I want to negate entire string.

I tried - inventory/[^getData](assumming it will check getData string not to be present in expression)

I want "inventory/getData" to fail above regex match whereas "inventory/get" to pass regex match. Basically want to negate string "getData" How can I do this?

  • 写回答

1条回答 默认 最新

  • down2323 2019-02-27 10:49
    关注

    If the entire string is inventory/getData or inventory/get then this will work as a positive match for inventory/get but not inventory/getData

    ^inventory/get$
    

    ^ matches the start of the string and $ matches the end

    This pattern asserts that the string ends with get, if it ends with getData it will not match


    To examine the more complex case of matching a negation, here are some example strings to be matched

    • 1) inventory/get
    • 2) inventory/getData
    • 3) inventory/snorkle
    • 4) inventory/getReal

    inventory/[^g][^e][^t][^D][^a][^t][^a] matches 3 only. This is because the [^g][^e][^t] section is a non match for the other examples

    inventory/(?:[^g][^e][^t][^D][^a][^t][^a]|get.*) matches all except for 2 -- not sure but perhaps this is what you are asking for? As you can see the negation is tailored to the test data

    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器