用于捕获目录和网站开头的正则表达式

Regular expression to capture start of directories and websites

我试图想出一个正则表达式来捕获字符串中 C:\H:/ 等的实例,但我遇到了问题。我已经尝试(在我的 Java 代码中)

的几种变体
line = line.replaceAll("[A-Z]:[/\\]", " ");

不过好像不行。我还需要匹配 https://,所以是否可以将其概括为匹配一组不分大小写的字母,后跟一个冒号,然后是 \/\,还是//

String resultString = subjectString.replaceAll("(?i)[a-z]:(\\|/)|https?://", " ");

将下面的字符串替换为</code> (space),不区分大小写:</p> <pre><code>c:\ d:/ http:// https://


正则表达式解释

[a-z]:(\\|/)|https?://

Options: Case insensitive; 

Match this alternative (attempting the next alternative only if this one fails) «[a-z]:(\\|/)»
   Match a single character in the range between “a” and “z” (case insensitive) «[a-z]»
   Match the character “:” literally «:»
   Match the regex below and capture its match into backreference number 1 «(\\|/)»
      Match this alternative (attempting the next alternative only if this one fails) «\\»
         Match the backslash character «\»
         Match the backslash character «\»
      Or match this alternative (the entire group fails if this one fails to match) «/»
         Match the character “/” literally «/»
Or match this alternative (the entire match attempt fails if this one fails to match) «https?://»
   Match the character string “http” literally (case insensitive) «http»
   Match the character “s” literally (case insensitive) «s?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the character string “://” literally «://»


Insert the character “ ” literally « »

此正则表达式模式适用于所有四种情况:[A-Z]:[/\]|https?://

它捕获一个大写字母,后跟 :,然后是 \/。或捕获字母 http,后跟或不跟 s,后跟 ://

工作示例:https://regex101.com/r/oH1aW7/1