如何在Java中检测URL末尾分页ID是否存在并获取其值
在Java中检测并提取URL末尾的整数分页ID
给定一批URL,部分末尾带有整数形式的分页ID,部分没有。需要实现逻辑判断URL末尾是否存在这类分页ID,存在则提取对应的数值。测试用的示例URL如下:
localhost:8200/videoslocalhost:8200/videos/1localhost:8200/videos/2localhost:8200/videos/popular-entertainmentlocalhost:8200/videos/popular-entertainment/1localhost:8200/videos/popular-entertainment/2localhost:8200/videos-funny/popular-entertainmentlocalhost:8200/videos-funny/popular-entertainment/1localhost:8200/videos-funny/popular-entertainment/2localhost:8200/videos-funny/popular-entertainment/3
下面提供两种可靠的实现方法:
方法一:正则表达式匹配
直接用正则定位URL末尾的整数部分,代码简洁高效。
实现代码
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PageIdExtractor { // 匹配末尾的/加数字 private static final Pattern PAGE_PATTERN = Pattern.compile("/(\\d+)$"); public static Integer getPageId(String url) { if (url == null || url.isBlank()) { return null; } Matcher matcher = PAGE_PATTERN.matcher(url); if (matcher.find()) { // 正则已经确保是数字,转Integer不会出错 return Integer.parseInt(matcher.group(1)); } return null; } // 测试用例 public static void main(String[] args) { String[] testUrls = { "localhost:8200/videos", "localhost:8200/videos/1", "localhost:8200/videos/2", "localhost:8200/videos/popular-entertainment", "localhost:8200/videos/popular-entertainment/1", "localhost:8200/videos/popular-entertainment/2", "localhost:8200/videos-funny/popular-entertainment", "localhost:8200/videos-funny/popular-entertainment/1", "localhost:8200/videos-funny/popular-entertainment/2", "localhost:8200/videos-funny/popular-entertainment/3" }; for (String url : testUrls) { Integer pageId = getPageId(url); System.out.printf("URL: %s → 分页ID: %s%n", url, pageId != null ? pageId : "不存在"); } } }
逻辑说明
- 正则
/(\\d+)$专门匹配URL结尾的/+数字组合,$保证是字符串最后一段,避免误匹配路径中间的数字。 - 匹配到后直接提取捕获组里的数字字符串,转成Integer返回;没匹配到就返回
null,表示没有分页ID。
方法二:拆分路径后校验最后一段
如果对正则不太熟悉,可以拆分URL路径,取最后一段判断是否为整数,逻辑更直观。
实现代码
public class PageIdExtractor { public static Integer getPageId(String url) { if (url == null || url.isBlank()) { return null; } // 先去掉查询参数(如果有) String path = url.split("\\?")[0]; // 按/拆分路径 String[] pathSegments = path.split("/"); String lastSegment = pathSegments[pathSegments.length - 1]; // 处理URL末尾带/的情况,比如"localhost:8200/videos/" if (lastSegment.isEmpty() && pathSegments.length > 1) { lastSegment = pathSegments[pathSegments.length - 2]; } try { return Integer.parseInt(lastSegment); } catch (NumberFormatException e) { // 最后一段不是整数,返回null return null; } } // 测试用例 public static void main(String[] args) { String[] testUrls = { "localhost:8200/videos", "localhost:8200/videos/1", "localhost:8200/videos/2", "localhost:8200/videos/popular-entertainment", "localhost:8200/videos/popular-entertainment/1", "localhost:8200/videos/popular-entertainment/2", "localhost:8200/videos-funny/popular-entertainment", "localhost:8200/videos-funny/popular-entertainment/1", "localhost:8200/videos-funny/popular-entertainment/2", "localhost:8200/videos-funny/popular-entertainment/3", "localhost:8200/videos/" // 测试末尾带/的场景 }; for (String url : testUrls) { Integer pageId = getPageId(url); System.out.printf("URL: %s → 分页ID: %s%n", url, pageId != null ? pageId : "不存在"); } } }
逻辑说明
- 先剥离URL的查询参数部分,只保留路径。
- 用
/把路径拆分成数组,取最后一段;如果最后一段是空(URL末尾带/),就取倒数第二段。 - 尝试把该段转成Integer,成功就返回数值,失败则说明不是分页ID,返回
null。
内容的提问来源于stack exchange,提问作者Adarsh Awasthi
相关产品推荐
相关产品推荐

