How to get regex match on my scenario

jacob

I want to match a url that has extra characters after a particular base url string. The base can be either '/home' or '/home/accounts/{numeric_account_id}'. I don't need to capture any matches; I just want to test whether there is a match.

I made a regex of:

r'^/home(/accounts/\d+)?(/[A-Za-z0-9_\-]+)+/?$'

Some examples are:

  1. '/home' => should not match
  2. '/home/' => should not match
  3. '/home/accounts/123' => should not match
  4. '/home/accounts/123/' => should not match
  5. '/home/x' => should match
  6. '/home/accounts/123/x' => should match

With my regex above, it incorrectly matches 3 & 4. I tried changing the '?' after accounts group to '*', but that doesn't help. It seems that the last group is greedily matching cases 3 & 4.

What is the correct regex? Thanks

Jerry

Try this regex:

^/home(?:(?!/accounts/)|/accounts/\d+)(?:/[A-Za-z0-9_\-]+)+/?$

It will match /home, then either:

(?!/accounts/): an empty string that doesn't have /accounts/ coming next, or

/accounts/\d+: /accounts/ followed by numbers.

After that, match any characters allowed in the path.

regex101 demo

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to modify my below regex?

来自分类Dev

How to dynamically create regex to use in .match Javascript?

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

Java Regex: How to Match URL Path?

来自分类Dev

Wrong RegEx match in Dart

来自分类Dev

Jquery Value match Regex

来自分类Dev

Regex multiple match substring

来自分类Dev

Search/Match regex in Python

来自分类Dev

How to run jbehave scenario multiple times sequentially

来自分类Dev

How do we solve the given scenario efficiently?

来自分类Dev

为什么[regex] match()和-match不同?

来自分类Dev

为什么[regex] match()和-match不同?

来自分类Dev

Regex to match and limit character classes

来自分类Dev

Shortest match in regex from end

来自分类Dev

std :: regex_match与字符éèà

来自分类Dev

C ++ regex_match行为

来自分类Dev

Codeigniter regex_match与比较

来自分类Dev

How do I get the nth derivative in my scheme program?

来自分类Dev

How do I get my aliases to have correct completion?

来自分类Dev

How do I get MVC to find my controller path?

来自分类Dev

How do I get my nested if statement to work in jQuery

来自分类Dev

How do I get `sbt test` to recognize my custom target?

来自分类Dev

Elegant way to use regex to match order-indifferent groups of characters (in a substring) while limiting how may times a given character can appear?

来自分类Dev

查找Regex.Match失败的地方

来自分类Dev

C ++无法使regex_match正常工作

来自分类Dev

PHP: Trying to understand subpattern regex match

来自分类Dev

Javascript regex match()返回匹配的部分(子集)

来自分类Dev

Using regex in Scala to group and pattern match

来自分类Dev

Regex to match the first half of a UK postcode

Related 相关文章

热门标签

归档