简体   繁体   中英

How can i get content between dates using java regular expressions

Below code did not get me the content between dates.can you please help me.

String res="05/22/2014 03:22:39.288 ffff gggg kkkkkk lllllll ssss 05/22/2014 03:22:39.288 oooooo ppppp qqqq rrrrrr sss 05/22/2014 03:22:39.378 mmmmmm nnn oooo ";   

String regEx="((0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d).*([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[\\d]*)(.*?)((0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d).*([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[\\d]*)?";

Matcher matcher = Pattern.compile(regEx).matcher(res);

while(matcher.find())
{
  String group = matcher.group();
  System.out.println(group);
}

Even i tried below using string split with regular expression in java but it gives only one result:

String regEx="((0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d).*([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].([\\d][^\\s]+)?)";
        String[] split = res.split(regEx);

You can use this regex to capture your text:

(?<=\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\.\d{3} )(.*?)(?=(?: \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\.\d{3}|$))

And grab your text in group #1 ie

matcher.group(1);

Working Demo

The reason you only get one result in the split is that the * operator is greedy in a regex. Take a read of this, it's good stuff: http://www.regular-expressions.info/repeat.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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