Skip to content

Commit

Permalink
Slightly modify regex to allow spaces in the beginning and ending of …
Browse files Browse the repository at this point in the history
…string.
  • Loading branch information
viirya committed Jul 14, 2015
1 parent 408b384 commit 815a9cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public final class Interval implements Serializable {
* Finally is the unit name, ends with an optional "s".
*/
private static String unitRegex(String unit) {
return "(?:\\s+(-?\\d+)\\s+" + unit + "s?)?";
return "(?:\\s+(-?\\d+)\\s+" + unit + "s?\\s*)?";
}

private static Pattern p = Pattern.compile("interval" + unitRegex("year") + unitRegex("month") +
private static Pattern p = Pattern.compile("\\s*interval" + unitRegex("year") + unitRegex("month") +
unitRegex("week") + unitRegex("day") + unitRegex("hour") + unitRegex("minute") +
unitRegex("second") + unitRegex("millisecond") + unitRegex("microsecond"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public void fromStringTest() {
Interval result = new Interval(-5 * 12 + 23, 0);
assertEquals(Interval.fromString(input), result);

input = "interval -5 years 23 month ";
assertEquals(Interval.fromString(input), result);

input = " interval -5 years 23 month ";
assertEquals(Interval.fromString(input), result);

// Error cases
input = "interval 3month 1 hour";
assertEquals(Interval.fromString(input), null);
Expand Down

0 comments on commit 815a9cb

Please sign in to comment.