我正在尝试找到以'/'例如'/'的文件夹的链接。www.w3.org/tr/owl-features/使用Java正则表达式,但我无法做到。任何Helpp都将不胜感激这是我的代码
File file4 = new File("W3C Web Pages");
File[] listOfFiles4 = file4.listFiles();
for (int i = 0; i < listOfFiles4.length; i++)
{
if (listOfFiles4[i].isFile())
{
FileReader filereader = new FileReader("W3C Web Pages/"+listOfFiles4[i].getName());
BufferedReader br = new BufferedReader(filereader);
Pattern regx = Pattern.compile("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
String text;
while ((text = br.readLine()) != null)
{
Matcher linkswslash = regx.matcher(text);
if (linkswslash.find())
{
if(linkswslash.group().contains("/"))
{
System.out.println("Found value: " + linkswslash.group());
}
}
}
}
}
为什么不将您的正则态度更改为"/",而其他URL则无法匹配,而不匹配"/"。这样的东西:
package com.example;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex1 {
public static void main(String[] args) {
System.out.println("in here");
String text = "https://www.w3.org/TR/owl-features/";
// wont' match https://www.w3.org/TR/owl-features
// but will match https://www.w3.org/TR/owl-features/
Pattern regx = Pattern.compile("(https?|ftp|file)://.*\/$");
Matcher linkswslash = regx.matcher(text);
if (linkswslash.find()){
System.out.println("Found value: " + linkswslash.group());
} else {
System.out.println("Not found");
}
}
}