正则表达式获取url:http://10.7.90.199:8032/bugfree/index.php/bug/293中的293
时间: 2024-11-12 14:34:40 浏览: 43
正则表达式可以用来从字符串中提取特定模式的信息,例如在这个URL中提取数字部分。针对你的需求,可以使用以下正则表达式:
http[s]?://[^/]+/(.*?)/\d+$
这个正则表达式的含义解释如下:
http[s]?://
匹配HTTP或HTTPS协议[^/]+
匹配除斜杠之外的一个或多个字符,即域名和端口号(.*?)
用于匹配任意字符直到遇到下一个斜杠/
/
然后匹配一个实际的斜杠\d+
最后匹配一个或多个数字,这正是我们要找的ID
在Java中,可以使用Pattern
和Matcher
类来应用这个正则表达式并获取数字:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String url = "http://10.7.90.199:8032/bugfree/index.php/bug/293";
Pattern pattern = Pattern.compile("http[s]?://[^/]+/(.*?)/\\d+");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
String id = matcher.group(1); // 提取数字
System.out.println("提取的数字ID: " + id); // 输出结果:293
} else {
System.out.println("未找到匹配的数字ID");
}
}
}
相关推荐

















