简体   繁体   English

从URL提取特定的字符串

[英]Extract specific string from URL

I want to extract some string from this url 我想从该网址中提取一些字符串

https://s3-ap-southeast-1.amazonaws.com/mtpdm/2019-06-14/12-14/1001_1203_20190614120605_5dd404.jpg https://s3-ap-southeast-1.amazonaws.com/mtpdm/2019-06-14/12-14/1001_1203_20190614120605_5dd404.jpg

I want to extract the 2019-06-14, how do I do that using java? 我想提取2019-06-14,如何使用Java做到这一点?

Use Regular Expression to achieve this to get the Date 2019-06-14 , 使用正则表达式来实现这一点,以获取Date 2019-06-14

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractDateFromURL {

     public static void main(String []args) {
        String URL = "https://s3-ap-southeast-1.amazonaws.com/mtpdm/2019-06-14/12-14/1001_1203_20190614120605_5dd404.jpg";

        Pattern pattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{1,2})");
        Matcher matcher = pattern.matcher(URL);

        if (matcher.find()) {
            System.out.println(matcher.group(1)); // Do what you need to do with the result
        }
     }
}

Output 输出量

2019-06-14 2019-06-14

And to get the 12-14 you can use the following Regular Expression, 要获得12-14您可以使用以下正则表达式,

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractDateFromURL {

     public static void main(String []args){
        String URL = "https://s3-ap-southeast-1.amazonaws.com/mtpdm/2019-06-14/12-14/1001_1203_20190614120605_5dd404.jpg";

        Pattern pattern = Pattern.compile("/(\\d{1,2}-\\d{1,2}-\\d{4}|\\d{1,2}-\\d{1,2})");
        Matcher matcher = pattern.matcher(URL);

        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
     }
}

Output 输出量

12-14 12-14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM