【问题标题】:Need help in writing Regular expression在编写正则表达式方面需要帮助
【发布时间】:2021-10-19 06:11:42
【问题描述】:

谁能帮我写正则表达式来匹配这种字符串

规则是:

  1. 应该以 nl 开头
  2. 在 nl 和 contact 之间可以有或不能有一个参数(如 /abc/),但不能有(/abc/def/)
  3. 接触后什么都可以

示例:

nl/abc/contact --> 允许

nl/contact --> 允许

nl/abc/def/contact --> 不允许

nl/abc/contact/mno --> 允许

nl/abc/contactmno/ ---> 不允许

我尝试写一个 ("^nl(.?)/contact(.)$"),但它的问题是它允许任意数量的斜杠在 nl 和联系方式之间,我只想在两者之间最多加一个斜线

【问题讨论】:

    标签: .net regex url-rewriting


    【解决方案1】:

    我会使用:

    ^nl(?:/[^/]+)?/contact(?:/[^/]+)*$
    

    Demo

    这个模式说匹配:

    ^            from the start of the path
    nl           starts with "nl"
    (?:/[^/]+)?  any zero or one path parameter following
    /contact     /contact
    (?:/[^/]+)*  followed by zero or more other path parameters
    $            end of the input
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多