regex,不是图像url的http链接
我在Sscala中解析文本并使用正则表达式:regex,不是图像url的http链接,regex,scala,Regex,Scala,我在Sscala中解析文本并使用正则表达式: val imageLink = "(http?:\\/\\/.*\\.(?:png|jpg|gif|bmp|jpeg))".r.findAllIn(postText).toList val htmlLink = "http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
val imageLink = "(http?:\\/\\/.*\\.(?:png|jpg|gif|bmp|jpeg))".r.findAllIn(postText).toList
val htmlLink = "http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
.r.findAllIn(postText).toList.filterNot(s => s.contains("jpg") || s.contains("jpeg")
|| s.contains("png") || s.contains("gif") || s.contains("bmp"))
但是我不想用所有的s。我想在正则表达式中找到不以jpg、bmp等结尾的http链接
谢谢我们的想法是使用负前瞻
(?!)
表达式:
"(?!.*(?:jpg|jpeg|png|gif|bmp))http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
您也可以省略http(s)
->https?
中的括号,因为?
在这两种情况下仅适用于s
字符
regexp的进一步改进是在URL中可能出现扩展名的确切位置检查扩展名。方法是使用负前瞻
(?!)
表达式:
"(?!.*(?:jpg|jpeg|png|gif|bmp))http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
您也可以省略http(s)
->https?
中的括号,因为?
在这两种情况下仅适用于s
字符
regexp的进一步改进是在URL中可能出现扩展的确切位置检查扩展