简体   繁体   English

正则表达式匹配字符串中的所有斜杠,如果一个单词出现在字符串中的任何位置

[英]Regex Match all the Slashes in String, If a Word Appears Anywhere in String

I am trying to match all the slashes / in a string but only if the string starts with "..我正在尝试匹配字符串中的所有斜杠/但前提是字符串以"..

This Regex only matches the first slash此正则表达式仅匹配第一个斜线

(?<=['|"]\.\.)(?<=.)*?/

Should match应该匹配

"../a/b/c/test.aspx"
"../a/test.aspx"

Should Not不应该

"/a/b/c/test.aspx"
../a/b/c/test.aspx

Any Ideas?有任何想法吗?

Notepad++ uses PCRE regex engine, hence you can use this regex: Notepad++ 使用 PCRE 正则表达式引擎,因此你可以使用这个正则表达式:

(?:^"\.\.|(?!^)\G)[^/\n]*\K/

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • (?: : Start non-capture group (?: : 启动非捕获组
    • ^"\.\. : Match ".. at the start ^"\.\. : 匹配"..在开头
    • | : OR : 要么
    • (?!^)\G : \G asserts position at the end of the previous match or the start of the string for the first match. (?!^)\G\G断言位置在上一个匹配项的末尾或第一个匹配项的字符串开头。 (?!^) makes sure we're not at the start position. (?!^)确保我们不在起始位置。
  • ) : End non-capture group ) : 结束非捕获组
  • [^/]* : Match 0 or more of any characters that is not / [^/]* :匹配 0 个或多个不是/的字符
  • \K : Reset all the matched info \K : 重置所有匹配的信息
  • / : Match a / / : 匹配一个/

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

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