The square brackets are used to include a set of characters (like "[A-Z]"). Replace those with parentheses:
"08 Nov 2022" -match '[0-3][0-9]\s(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}'
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I was asked to write a regular expression for "08 Nov 2022". The day has to be 2 digits, the month has to be 3 letters, and the year has to be 4 digits. You also have to specify a space inbetween the day, month, and year
I came up with: "08 Nov 2022" -match '[0-3][0-9]\s[jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec]\s\d{4}'
When this is executed, it will return false.
I tried the following, each returned true:
"08" -match '[0-3][0-9]'
"Nov" -match '[jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec]'
"2022" -match '\d{4}'
"08 Nov" -match '[0-3][0-9]\s[jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec]'
"Nov 2022" -match '[jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec]\s\d{4}'
But when you combine everything to validate day, month, and year; it returns false.
Does anyone have an idea why this is happening? I have a few friends also looking into this but they don't have an answer for me.
The square brackets are used to include a set of characters (like "[A-Z]"). Replace those with parentheses:
"08 Nov 2022" -match '[0-3][0-9]\s(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{4}'