我有一个网址:
/冰淇淋/东西/夹层/香蕉
如果满足以下条件,我想编写一个仅与URL匹配的正则表达式:
我试过这个: 冰淇淋。三明治。 ^ [(香蕉)]以及许多其他人但尚未找到解决方案。
帮助很感激。
答案 0 :(得分:1)
试试下面的正则表达式,
^(?!.*banana.*).*?ice-cream.*?sandwich.*$
OR
^(?!.*banana.*)(?:(?!sandwich).)*ice-cream.*?sandwich.*$
<强>解释强>
^
断言我们在行的开头。(?!.*banana.*)
检查该行的否定前瞻包含字符串banana。如果不是,那么正则表达式引擎会在启动时设置标记。或者,它会跳过包含字符串banana
。(?:(?!sandwich).)*
匹配所有不属于字符串sandwich
的字符。ice-cream.*?sandwich.*
字符串三明治必须在字符串ice-cream
之后。$
行尾。答案 1 :(得分:1)
如果没有匹配和不匹配的示例,很难准确,但尝试一下:
^(?!.*banana)(?:(?!.*sandwich(?=.*ice-cream))).*ice-cream.*sandwich.*$
正则表达式的解释:
^(?!.*banana)(?:(?!.*sandwich(?=.*ice-cream))).*ice-cream.*sandwich.*$
----------------------------------------------------------------------
^(?!.*banana)(?:(?!.*sandwich(?=.*ice-cream))).*ice-cream.*sandwich.*$
Options: Case insensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Default line breaks
Assert position at the beginning of a line «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*banana)»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character string “banana” literally «banana»
Match the regular expression below «(?:(?!.*sandwich(?=.*ice-cream)))»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*sandwich(?=.*ice-cream))»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character string “sandwich” literally «sandwich»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*ice-cream)»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character string “ice-cream” literally «ice-cream»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character string “ice-cream” literally «ice-cream»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character string “sandwich” literally «sandwich»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of a line «$»
Created with RegexBuddy
答案 2 :(得分:-1)
.*ice-cream.+sandwich.(?!banana).*
试试这个