(Java/Regex)如何找到“http://”除了“src=”http://”
我现在对Java上的正则表达式有问题
我有这样的字符串:
img border=\"0\" src=\"http://www.stackoverflow.com http://nbsp;https://<
并且想做一个正则表达式,只找到两个“http://”,除了“src=\”http://”,以替换“http://”为其他内容
String input = "border=\"0\" src=\"http://www.stackoverflow.com http://";
String regexStr = "(?!src=\"http://).*$";
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(input);
if (matcher.matches())
System.out.println("String " + input + " has the word src=\"http:// in it ");
else
System.out.println("String " + input + " hasn't the word src=\"http:// in it ");
我正在搜索与此相关的内容,但尚未找到完美的答案
如有任何意见,将不胜感激。谢谢
# 1 楼答案
您需要的正则表达式是
括号中的部分是“负面回顾”。它的意思是前面没有
src="
当然,当您用Java字符串文字编写此代码时,它必须是
# 2 楼答案
您可以使用以下正则表达式:
与中定义的URL匹配的众多正则表达式之一 What is a good regular expression to match a URL?
输入:
匹配项:
演示:https://regex101.com/r/SOyEtd/2
如果只想获取
http://
,请仅使用:演示:https://regex101.com/r/SOyEtd/4
嵌入Java:
输出: