<p>What is the RegEx pattern for DateTime (2008-09-01 12:35:45 ) ?</p>

<p>I get this error:</p>

<blockquote>
  <p>No ending delimiter '^' found</p>
</blockquote>

<p>Using:</p>

<pre><code>preg_match('(?n:^(?=\d)((?&lt;day&gt;31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|1\d|2[0-8])(?&lt;sep&gt;[/.-])(?&lt;month&gt;0?[1-9]|1[012])\2(?&lt;year&gt;(1[6-9]|[2-9]\d)\d{2})(?:(?=\x20\d)\x20|$))?(?&lt;time&gt;((0?[1-9]|1[012])(:[0-5]\d){0,2}(?i:\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)', '2008-09-01 12:35:45');
</code></pre>

<p>Gives this error: </p>

<blockquote>
  <p>Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 0 in E:\www\index.php on line 19</p>
</blockquote>

[{'answer_id': 37742, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://regexlib.com/REDetails.aspx?regexp_id=610" rel="noreferrer">http://regexlib.com/REDetails.aspx?regexp_id=610</a></p>\n\n<blockquote>\n  <p>^(?=\\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\\x20|$))|(?:2[0-8]|1\\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\\1(?:1[6-9]|[2-9]\\d)?\\d\\d(?:(?=\\x20\\d)\\x20|$))?(((0?[1-9]|1[012])(:[0-5]\\d){0,2}(\\x20[AP]M))|([01]\\d|2[0-3])(:[0-5]\\d){1,2})?$</p>\n</blockquote>\n\n<hr>\n\n<blockquote>\n  <p>This RE validates both dates and/or\n  times patterns. Days in Feb. are also\n  validated for Leap years. Dates: in\n  dd/mm/yyyy or d/m/yy format between\n  1/1/1600 - 31/12/9999. Leading zeroes\n  are optional. Date separators can be\n  either matching dashes(-), slashes(/)\n  or periods(.) Times: in the hh:MM:ss\n  AM/PM 12 hour format (12:00 AM -\n  11:59:59 PM) or hh:MM:ss military time\n  format (00:00:00 - 23:59:59). The 12\n  hour time format: 1) may have a\n  leading zero for the hour. 2) Minutes\n  and seconds are optional for the 12\n  hour format 3) AM or PM is required\n  and case sensitive. Military time 1)\n  must have a leading zero for all hours\n  less than 10. 2) Minutes are\n  manditory. 3) seconds are optional.\n  Datetimes: combination of the above\n  formats. A date first then a time\n  separated by a space. ex) dd/mm/yyyy\n  hh:MM:ss</p>\n</blockquote>\n\n<hr>\n\n<p><strong>Edit</strong>: Make sure you copy the RegEx from the regexlib.com website as StackOverflow sometimes removes/destroys special chars. </p>\n'}, {'answer_id': 37758, 'author': 'Tom', 'author_id': 3715, 'author_profile': 'https://Stackoverflow.com/users/3715', 'pm_score': 5, 'selected': False, 'text': '<p>A simple version that will work for the format mentioned, but not all the others as per @Espos:</p>\n\n<pre><code>(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}) \n</code></pre>\n'}, {'answer_id': 37767, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 7, 'selected': True, 'text': "<p>@Espo: I just have to say that regex is incredible. I'd hate to have to write the code that did something useful with the matches, such as if you wanted to actually find out what date and time the user typed.</p>\n\n<p>It seems like Tom's solution would be more tenable, as it is about a zillion times simpler and with the addition of some parentheses you can easily get at the values the user typed:</p>\n\n<pre><code>(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})\n</code></pre>\n\n<p>If you're using perl, then you can get the values out with something like this:</p>\n\n<pre><code>$year = $1;\n$month = $2;\n$day = $3;\n$hour = $4;\n$minute = $5;\n$second = $6;\n</code></pre>\n\n<p>Other languages will have a similar capability. Note that you will need to make some minor mods to the regex if you want to accept values such as single-digit months.</p>\n"}, {'answer_id': 37875, 'author': 'Imran', 'author_id': 1897, 'author_profile': 'https://Stackoverflow.com/users/1897', 'pm_score': -1, 'selected': False, 'text': '<p>PHP preg functions needs your regex to be wrapped with a delimiter character, which can be any character. You can\'t use this delimiter character without escaping inside the regex. This should work (here the delimiter character is /):</p>\n\n<pre><code>preg_match(\'/\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}/\', \'2008-09-01 12:35:45\');\n\n// or this, to allow matching 0:00:00 time too.\npreg_match(\'/\\d{4}-\\d{2}-\\d{2} \\d{1,2}:\\d{2}:\\d{2}/\', \'2008-09-01 12:35:45\');\n</code></pre>\n\n<p>If you need to match lines that contain only datetime, add ^ and $ at the beginning and end of the regex.</p>\n\n<pre><code>preg_match(\'/^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$/\', \'2008-09-01 12:35:45\');\n</code></pre>\n\n<p><a href="http://www.php.net/preg-match" rel="nofollow noreferrer">Link to PHP Manual\'s preg_match()</a></p>\n'}, {'answer_id': 4727571, 'author': 'deni', 'author_id': 580368, 'author_profile': 'https://Stackoverflow.com/users/580368', 'pm_score': 1, 'selected': False, 'text': "<p>regarding to Imran answer from Sep  1th 2008 at 12:33\nthere is a missing : in the pattern\nthe correct patterns are</p>\n\n<pre><code>preg_match('/\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}/', '2008-09-01 12:35:45', $m1);\nprint_r( $m1 );\npreg_match('/\\d{4}-\\d{2}-\\d{2} \\d{1,2}:\\d{2}:\\d{2}/', '2008-09-01 12:35:45', $m2);\nprint_r( $m2 );\npreg_match('/^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$/', '2008-09-01 12:35:45', $m3);\nprint_r( $m3 );\n</code></pre>\n\n<p>this returns</p>\n\n<pre><code>Array ( [0] =&gt; 2008-09-01 12:35:45 )\nArray ( [0] =&gt; 2008-09-01 12:35:45 )\nArray ( [0] =&gt; 2008-09-01 12:35:45 ) \n</code></pre>\n"}, {'answer_id': 11794812, 'author': 'Javad Yousefi', 'author_id': 616493, 'author_profile': 'https://Stackoverflow.com/users/616493', 'pm_score': 2, 'selected': False, 'text': '<pre><code>^([2][0]\\d{2}\\/([0]\\d|[1][0-2])\\/([0-2]\\d|[3][0-1]))$|^([2][0]\\d{2}\\/([0]\\d|[1][0-2])\\/([0-2]\\d|[3][0-1])\\s([0-1]\\d|[2][0-3])\\:[0-5]\\d\\:[0-5]\\d)$\n</code></pre>\n'}, {'answer_id': 22798655, 'author': 'Philip', 'author_id': 866466, 'author_profile': 'https://Stackoverflow.com/users/866466', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$date = "2014-04-01 12:00:00";\n\npreg_match(\'/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/\',$date, $matches);\n\nprint_r($matches);\n</code></pre>\n\n<p>$matches will be:</p>\n\n<pre><code>Array ( \n   [0] =&gt; 2014-04-01 12:00:00 \n   [1] =&gt; 2014 \n   [2] =&gt; 04 \n   [3] =&gt; 01 \n   [4] =&gt; 12 \n   [5] =&gt; 00 \n   [6] =&gt; 00\n)\n</code></pre>\n\n<p>An easy way to break up a datetime formated string.</p>\n'}, {'answer_id': 24689706, 'author': 'Duc Tran', 'author_id': 617146, 'author_profile': 'https://Stackoverflow.com/users/617146', 'pm_score': -1, 'selected': False, 'text': "<p>Here is a simplified version (originated from Espo's answer). It checks the correctness of date (even leap year), and  hh:mm:ss is optional\n<br/> Examples that work:\n<br/>- 31/12/2003 11:59:59\n<br/>- 29-2-2004</p>\n\n<pre><code>^(?=\\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\\x20|$))|(?:2[0-8]|1\\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\\1(?:1[6-9]|[2-9]\\d)?\\d\\d(?:(?=\\x20\\d)\\x20|$))(|([01]\\d|2[0-3])(:[0-5]\\d){1,2})?$\n</code></pre>\n"}, {'answer_id': 32867745, 'author': 'Tornike', 'author_id': 413026, 'author_profile': 'https://Stackoverflow.com/users/413026', 'pm_score': 0, 'selected': False, 'text': '<p>Here is my solution:</p>\n\n<pre><code>/^(2[0-9]{3})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) (0[0-9]|1[0-9]|2[0123])\\:([012345][0-9])\\:([012345][0-9])$/u\n</code></pre>\n'}, {'answer_id': 40124570, 'author': 'Yeongjun Kim', 'author_id': 3793078, 'author_profile': 'https://Stackoverflow.com/users/3793078', 'pm_score': -1, 'selected': False, 'text': '<p>Here is my solution:</p>\n\n<pre><code>[12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]) ([01][0-9]|2[0-3]):[0-5]\\d\n</code></pre>\n\n<p><img src="https://www.debuggex.com/i/39LWCacr76aGZfCN.png" alt="Regular expression visualization"></p>\n\n<p><a href="https://www.debuggex.com/r/39LWCacr76aGZfCN" rel="nofollow noreferrer">Debuggex Demo</a></p>\n\n<p><a href="https://regex101.com/r/lbthaT/4" rel="nofollow noreferrer">https://regex101.com/r/lbthaT/4</a></p>\n'}, {'answer_id': 40842114, 'author': 'themepark', 'author_id': 7086012, 'author_profile': 'https://Stackoverflow.com/users/7086012', 'pm_score': 0, 'selected': False, 'text': '<p>I have modified the regex pattern from <a href="http://regexlib.com/REDetails.aspx?regexp_id=610" rel="nofollow noreferrer">http://regexlib.com/REDetails.aspx?regexp_id=610</a>. The following pattern should match your case.</p>\n\n<pre><code>^(?=\\d)(?:(?:1[6-9]|[2-9]\\d)?\\d\\d([-.\\/])(?:1[012]|0?[1-9])\\1(?:31(?&lt;!.(?:0[2469]|11))|(?:30|29)(?&lt;!.02)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\\x20|$))|(?:2[0-8]|1\\d|0?[1-9]))(?:(?=\\x20\\d)\\x20|$))?(((0?[1-9]|1[012])(:[0-5]\\d){0,2}(\\x20[AP]M))|([01]\\d|2[0-3])(:[0-5]\\d){1,2})?$\n</code></pre>\n\n<p>YYYY-MM-DD HH:MM:SS</p>\n'}, {'answer_id': 61888323, 'author': 'Milkncookiez', 'author_id': 1430394, 'author_profile': 'https://Stackoverflow.com/users/1430394', 'pm_score': 1, 'selected': False, 'text': '<p>Adding to @Greg Hewgill answer: if you want to be able to match both date-time and <em>only</em> date, you can make the "time" part of the regex optional:</p>\n\n<pre><code>(\\d{4})-(\\d{2})-(\\d{2})( (\\d{2}):(\\d{2}):(\\d{2}))?\n</code></pre>\n\n<p>this way you will match both <code>2008-09-01 12:35:42</code> and <code>2008-09-01</code></p>\n'}, {'answer_id': 73302581, 'author': 'Seongjoo Park', 'author_id': 11589521, 'author_profile': 'https://Stackoverflow.com/users/11589521', 'pm_score': 1, 'selected': False, 'text': "<p>simple regex datetime with validation</p>\n<pre><code>^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$\n</code></pre>\n<p>but it can't validation month which not include 31day (ex. 2022-11-31, 2022-02-30)</p>\n"}, {'answer_id': 74500646, 'author': 'ali t', 'author_id': 6028779, 'author_profile': 'https://Stackoverflow.com/users/6028779', 'pm_score': 0, 'selected': False, 'text': '<p>This is my soloution:</p>\n<pre><code>[1-9][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[0-1])\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/37732', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4013/']