简体   繁体   English

URL匹配正则表达式模式

[英]URL match regex pattern

I have a bunch of URLs that I need to filter out, based on whether it contains the keyword 'staff' 根据是否包含关键字'staff' ,我需要过滤一堆网址

1. /services
2. /services/EarNoseThroat
3. /services/EarNoseThroat/Audiology
4. /services/EarNoseThroat/Audiology/CochlearImplant
5. /services/BehavioralHealth/Clinic  

6. /services/BehavioralHealth/Clinic/staff
7. /services/BehavioralHealth/Clinic/staff/Jamie-Hudgins

I want to create one regex pattern to match all the URLs that have /services after the host URL, but not 'staff' anywhere in the URL. 我想创建一个正则表达式模式,以匹配主机URL之后具有/services所有URL,而不匹配URL中任何位置的'staff' Basically match URLS 1 to 5. 基本上将网址1匹配到5。

I also need a pattern than only match URL 6 and 7. 除了只匹配URL 6和7,我还需要一种模式。

It seems like the negative lookahead will do the trick, except I don't know how to put it together. 否定的前瞻似乎可以解决问题,除非我不知道如何将其组合在一起。 Can someone help me out? 有人可以帮我吗?

Something like: 就像是:

^\/services\/(?:[^\/]+\/?)*$


OR

^/services\/...any Depth here...\/(?!staff)

Regex to match the following: 正则表达式以匹配以下内容:

/services
/services/EarNoseThroat
/services/EarNoseThroat/Audiology
/services/EarNoseThroat/Audiology/CochlearImplant
/services/BehavioralHealth/Clinic  

Regex: 正则表达式:

^\/services\/(?!.*\bstaff\b).*$

Click for Demo 点击演示

Explanation: 说明:

  • ^ - asserts the start of the string ^ -断言字符串的开头
  • \\/services\\/ - matches /services/ \\/services\\/ -匹配/services/
  • (?!.*\\bstaff\\b) - negative lookahead to make sure that the word staff does not appear anywhere in the string (?!.*\\bstaff\\b) - (?!.*\\bstaff\\b)否定以确保单词staff不会出现在字符串中的任何位置
  • .* - matches 0+ occurrences of any character except a newline character .* -匹配0+次出现的除换行符以外的任何字符
  • $ - asserts the end of string $ -断言字符串的结尾

Regex to match the following: 正则表达式以匹配以下内容:

/services/BehavioralHealth/Clinic/staff
/services/BehavioralHealth/Clinic/staff/Jamie-Hudgins

Regex: 正则表达式:

^\/services\/(?=.*\bstaff\b).*$

Click for Demo 点击演示

Explanation: 说明:

The only difference is the positive lookahead: 唯一的区别是正面的前瞻性:

  • (?=.*\\bstaff\\b) - positive lookahead to make sure that the word staff appears somewhere in the string before the end of the string (?=.*\\bstaff\\b) -正向超前以确保单词staff出现在字符串末尾之前的某个位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM