2

Regex:

Nov/([0-3][0-9])

Tests:

  1. Wed Nov/21/2012 - ... ===> Nov/21 OK
  2. Wed 21/Nov/2012 - ... ===> Nov/20 BAD

Is there a way to ignore the second case with regex?

(The only method I can think of is to add a single space before the current regex but that feels like a hack and I was wondering if there is a proper approach)

I'm doing this with Java (java.util.regex package).

takfuruya
  • 2,453
  • 2
  • 15
  • 10
  • 1
    Which regex engine are you using? Some have look-ahead capability, which might well be of use to you. – ClickRick Apr 12 '14 at 15:21
  • Some of the tricks that might be useful here are not available in all regex implementations. Please tell us the language or tool you are going to execute this regex in so that we can tailor answers appropriately. – IMSoP Apr 12 '14 at 15:23
  • 1
    I'm using Java (java.util.regex package). I'm not sure what engine it uses by default. – takfuruya Apr 12 '14 at 15:24

3 Answers3

2

You can use a negative lookahead to filter out the second case:

Nov/([0-3][0-9])(?![0-9]{2})

Here is a fiddle where you can check it out.

Note that with java.util.regex's Matcher class, you will want to use find() rather than matches() in this case. You will notice this in the fiddle, and another SO post explains why very well.

Community
  • 1
  • 1
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
1

Negative lookahead is overkill here. I always prefer to stick with vanilla regex features when possible so it will be easier for others to understand and more portable. You are capturing the day of the month already in the group ([0-9][0-9]). If extracting that is your purpose, I see no reason not to make your whole regex more explicit about what surrounds the day of the month. Your instinct to add a leading space (or, equivalently, a trailing slash) is exactly what I'd do if I were composing the regex on the command line (e.g. for sed or perl). It seeems slightly cleaner to me to go ahead and express the entire date portion you are searching for by spelling out the year: Nov/([0-3][0-9])/[0-9][0-9][0-9][0-9]. The extra characters will not affect how you use the captured day of month.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
0

Do you want to check for a valid date in November month?

Try this one.

                            //Nov/two digits/four digits
Pattern p = Pattern.compile("(Nov/\\d{2}/\\d{4})");
//Try this one if you want to validate date also
//Pattern p = Pattern.compile("(Nov/(0?[1-9]|[12][0-9]|30)/((19|20)\\d\\d))$");
String[] values = new String[] { "Wed Nov/21/2012", "Wed 21/Nov/2012" };
for (String value : values) {
    Matcher m = p.matcher(value);
    if (m.find()) {
        System.out.println(value + " found");
    } else {
        System.out.println(value + " not found");
    }
}

output:

Wed Nov/21/2012 found
Wed 21/Nov/1012 not found

For more info please find samples here.

Braj
  • 46,415
  • 5
  • 60
  • 76