gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:44:58 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

将下面的字符串替换为 (空格),不区分大小写:

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 « »

关于java - 用于捕获目录和网站开头的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37554433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com