IntelliJ IDEA tip: check regular expressions right in the IDE

Photo by Hedgehog Digital on Unsplash
scala> "hamburger".indexOf("burger")
res0: Int = 3
scala> "hot dogs".indexOf("burger")
res1: Int = -1
    // STUB TO FAIL THE FIRST TEST
public static LocalDate findCalendarDate(String s) {
return LocalDate.of(1970, 1, 1);
}
    @Test
// Add "public" if using JUnit 4
void testFindCalendarDate() {
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMMM d, yyyy");
LocalDate expected = LocalDate.now();
String s = "Performing test on "
+ expected.format(formatter);
System.out.println(s);
LocalDate actual = RegexPractice.findCalendarDate(s);
assertEquals(expected, actual);
}
    @Test
// Add "public" if using JUnit 4
void testFindOtherCalendarDate() {
Random random = new Random();
int numberOfDaysAgo = random.nextInt(635) + 365;
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MMMM d, yyyy");
LocalDate expected
= LocalDate.now().minusDays(numberOfDaysAgo);
String s = numberOfDaysAgo + " days ago was "
+ expected.format(formatter);
System.out.println(s);
LocalDate actual = RegexPractice.findCalendarDate(s);
assertEquals(expected, actual);
}
        Pattern pattern = Pattern.compile("REG EXP GOES HERE");
Pattern pattern = Pattern.compile("January|February|March|April|May|
June|July|August|September|October|November|December");
IntelliJ IDEA Check RegExp frame showing a matching regular expression: “February” matches a regular expression for the twelve months of the year.
IntelliJ IDEA Check RegExp frame showing a matching regular expression: “February” matches a regular expression for the twelve months of the year.
IntelliJ IDEA Check RegExp frame showing a matching regular expression.
IntelliJ IDEA Check RegExp frame showing a sample that doesn’t match the given regular expression, in this case, “Feb.” doesn’t match against the names of the twelve months of the year written out in full.
IntelliJ IDEA Check RegExp frame showing a sample that doesn’t match the given regular expression, in this case, “Feb.” doesn’t match against the names of the twelve months of the year written out in full.
IntelliJ IDEA Check RegExp frame showing a sample that doesn’t match the given regular expression.
Pattern pattern = Pattern.compile("January|February|March|April|May|
June|July|August|September|October|November|December \\d{1,2}");
Pattern pattern = Pattern.compile("(January|February|March|April|
May|June|July|August|September|October|November|December) \\d{1,2}");
Pattern pattern = Pattern.compile("(January|February|March|April|
May|June|July|August|September|October|November|December) \\d{1,2}, 20\\d\\d");
Pattern pattern = Pattern.compile("(January|February|March|"
+ "April|May|June|July|August|September|October|"
+ "November|December) \\d{1,2}, 20\\d\\d");
IntelliJ IDEA Check RegExp frame showing a matching regular expression. “June 6, 2012” matches a regular expression for a month name, space, one or two digits, comma, space, and a year from 2000 to 2099.
IntelliJ IDEA Check RegExp frame showing a matching regular expression. “June 6, 2012” matches a regular expression for a month name, space, one or two digits, comma, space, and a year from 2000 to 2099.
IntelliJ IDEA Check RegExp frame showing a matching regular expression.
    private static final DateTimeFormatter DATE_FORMATTER 
= DateTimeFormatter.ofPattern("MMMM d, yyyy");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
return LocalDate.parse(matcher.group(), DATE_FORMATTER);
} else {
return LocalDate.now(); // FAIL THE NO MATCHES TEST
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store