需要帮助编写正则表达式
Need help in writing Regular expression
谁能帮我写正则表达式来匹配这种字符串
规则是:
- 应该以nl开头
- 它可以或不能在 nl 和联系人之间有一个参数(如 /abc/)但不能(/abc/def/)
- 接触后什么都有
示例:
nl/abc/contact --> 允许
nl/contact --> 允许
nl/abc/def/联系人 --> 不允许
nl/abc/contact/mno --> 允许
nl/abc/contactmno/ ---> 不允许
我试着写了一个 ("^nl(.?)/contact(.)$"),但它有一个问题是它允许在 nl 和 contact wheras 之间有任意数量的斜线 我只想在两者之间最多一个斜线
我会使用:
^nl(?:/[^/]+)?/contact(?:/[^/]+)*$
这个模式说要匹配:
^ 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
谁能帮我写正则表达式来匹配这种字符串
规则是:
- 应该以nl开头
- 它可以或不能在 nl 和联系人之间有一个参数(如 /abc/)但不能(/abc/def/)
- 接触后什么都有
示例:
nl/abc/contact --> 允许
nl/contact --> 允许
nl/abc/def/联系人 --> 不允许
nl/abc/contact/mno --> 允许
nl/abc/contactmno/ ---> 不允许
我试着写了一个 ("^nl(.?)/contact(.)$"),但它有一个问题是它允许在 nl 和 contact wheras 之间有任意数量的斜线 我只想在两者之间最多一个斜线
我会使用:
^nl(?:/[^/]+)?/contact(?:/[^/]+)*$
这个模式说要匹配:
^ 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