首页  编辑  

Java中使用正则表达式快速提取需要的数据

Tags: /Android/   Date Created:
举个例子,要快速提取下面的字符串中的数据:
rtsp://admin:123456@192.168.1.111:554/h264/main/ch1/av_stream
得到 admin, 123456, 192.168.1.111, 554, h264/main/ch1/av_stream 这几块的数据,可以用正则表达式轻松实现:
private static final Pattern regURI = Pattern.compile("rtsp://(\\S+):(.+)@(.+):(\\d+)/(\\S+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = regURI.matcher("rtsp://admin:123456@192.168.1.111:554/h264/main/ch1/av_stream");
if (!matcher.find()) {
  Log.i("OK", "user: " + matcher.group(1) + " password: " + matcher.group(2) + " server: " + matcher.group(3) + " port: " + matcher.group(4) + " resource: " + matcher.group(5));
}