Created
July 13, 2020 09:52
-
-
Save abhisheksharma14/32fa2c7a1e988744f2d8e1af617abef4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find distinct years | |
/* | |
Find all the distinct years present in the a given string. | |
*/ | |
import java.util.*; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Main | |
{ | |
public static void main(String[] args) { | |
final String string = "the date 10-12-1996 is ahead of 10-12-1995 but befor 10-12-2001"; | |
final String regex = "[0-9]{2}-[0-9]{2}-[0-9]{4}"; | |
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); | |
final Matcher matcher = pattern.matcher(string); | |
Set<String> years = new HashSet<String>(); | |
while (matcher.find()) { | |
String date = matcher.group(0); | |
String year = date.substring(6, date.length()); | |
years.add(year); | |
} | |
System.out.println(years); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment