I have a regex to match the date formats Sep.23'15 or Sep 23'15 or Sep23'15
[a-zA-Z]{3}[. ]\d{2}'\d{2} I am able to match Sep.23'15 & Sep 23'15 but not Sep23'15
How to write the regex to match with space and without space ?
I suggest matching a dot (optionally) and then use * quantifier instead of the ? suggested by Tushar applied to a space:
[a-zA-Z]{3}\.?[ ]*\d{2}'\d{2} ^^^^^^^ This regex will also handle format like Sep. 23'15 (with a dot and a space(s) between the month and the day'year).
Regex explanation:
[a-zA-Z]{3} - 3 ASCII letters\.? - 1 or 0 dots[ ]* - zero or more regular spaces (\h, or \p{Zs}, or [[:blank:]] are recommended depending on the regex flavor if you only need to match horizontal whitespace)\d{2}'\d{2} - 2 digits + ' + 2 digits.See demo
* (incorrect, as it allows Sep.......23'15 as valid) but ? (correct) in your demo... why?Sep, 23'15 (with several blanks after the dot) better would be (\.|[ ]*) which allows either one dot or a sequence of 0 or more spaces, but not both.You can use the following regex:
[A-Za-z]{3}[\.\s]?\d{1,2}\'\d{2} . symbol, no need escaping it. Also, OP might not want to match any whitespace (\s also matches newline symbols).
?for[. ]. Answer:[a-zA-Z]{3}[. ]?\d{2}'\d{2}