这是我的面试问题之一。我没有想出足够好的解决方案并被拒绝了。
问题是
What is the one regex to match all urls that contain job(case insensitive) in the relative
path(not domain) in the following list:
- http://www.glassdoor.com/job/ABC
- https://glassdoor.com/job/
- HTTPs://job.com/test
- Www.glassdoor.com/foo/bar/joBs
- http://192.168.1.1/ABC/job
- http://bankers.jobs/ABC/job
我的解决方案是使用
lookahead
和lookbehind
,/(?<!\.)job(?!\.)/i
。这在上面的列表中效果很好。但是,如果 url 是 HTTPs://jobs.com/test
,则不起作用。
我想知道这个问题的正确答案是什么。预先感谢您的任何建议!
如果您不需要验证网址,只需关注“job”
# /(?i)(?<=\/)job(?=\/|[^\S\r\n]*$)/
(?i)
(?<= / )
job
(?= / | [^\S\r\n]* $ )
尝试这个正则表达式:
\b(?:https?:\/\/)?[^\/:\n]+\/.*?job
正则表达式详细信息:
\b
:单词边界(?:https?:\/\/)?
:匹配可选 http://
或 https://
[^\/:]+
:匹配 1 个以上非 /
和 :
\/
:匹配 /
.*?job
:匹配 0 个或多个字符,后跟文本 job
这是我想出的一个:
^(?:.*://)?(?:[wW]{3}\.)?([^:/])*/.*job.*
它匹配您的所有示例,但不匹配带有 job.com 或 jobs.com 的示例。 (工作仅在路径中。)
我在 sublime text 中对此进行了测试,这很好,因为正则表达式结果会在您键入时突出显示。
我在面试时也被问到这个问题,这是我的解决方案: /./+job/?./i 它在 Rubular.com 上运行良好