Regular Expression to match relative URLs

删除回忆录丶 提交于 2019-12-03 21:16:01

Try this regular expression:

/abc/(?:[A-Za-z0-9-._~!$&'()*+,;=:@]|%[0-9a-fA-F]{2})*(?:/(?:[A-Za-z0-9-._~!$&'()*+,;=:@]|%[0-9a-fA-F]{2})*)*

This is derived from the ABNF of a generic URI.

/^\/abc\//

However my guess is you are missing something in the question....

The answer I write here is for PHP; at the moment I am writing it, you didn't report for which programming language you are interested to have an answer.

  • If you want a regular expression to check if the path starts with /abc/

    preg_match('|^/abc/.*$|i', $path);

  • If you want a regular expression that returns the part after /abc/

    preg_match('|^/abc/(.*)$|i', $path, $matches);

In both the cases, regular expressions can be avoided; you can use string functions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!