<p>I want to create a regexp in Emacs that matches exactly 3 digits.   For example, I want to match the following:</p>

<pre><code>123
345
789
</code></pre>

<p>But not</p>

<pre><code>1234
12
12 23
</code></pre>

<p>If I use <code>[0-9]+</code> I match any single string of digits.   I thought <code>[0-9]{3}</code> would work, but when tested in re-builder it doesn't match anything.</p>

[{'answer_id': 69593, 'author': 'mike511', 'author_id': 9593, 'author_profile': 'https://Stackoverflow.com/users/9593', 'pm_score': -1, 'selected': False, 'text': "<p>It's pretty simple:</p>\n\n<pre><code>[0-9][0-9][0-9]\n</code></pre>\n"}, {'answer_id': 69615, 'author': 'Joe Hildebrand', 'author_id': 8388, 'author_profile': 'https://Stackoverflow.com/users/8388', 'pm_score': 7, 'selected': True, 'text': '<p>If you\'re entering the regex interactively, and want to use <code>{3}</code>, you need to use backslashes to escape the curly braces.  If you don\'t want to match any part of the longer strings of numbers, use <code>\\b</code> to match word boundaries around the numbers.  This leaves:</p>\n\n<pre><code>\\b[0-9]\\{3\\}\\b\n</code></pre>\n\n<p>For those wanting more information about <code>\\b</code>, see <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Backslash.html#Regexp-Backslash" rel="noreferrer">the docs</a>:</p>\n\n<blockquote>\n  <p>matches the empty string, but only at the beginning or end of a word. Thus, <code>\\bfoo\\b</code>\n  matches any occurrence of <code>foo</code> as a separate word. \n  <code>\\bballs?\\b</code> matches <code>ball</code> or <code>balls</code> as a separate word.\n  <code>\\b</code> matches at the beginning or end of the buffer regardless of what text appears next to it. </p>\n</blockquote>\n\n<p>If you do want to use this regex from elisp code, as always, you must escape the backslashes one more time.  For example:</p>\n\n<pre><code>(highlight-regexp "\\\\b[0-9]\\\\{3\\\\}\\\\b")\n</code></pre>\n'}, {'answer_id': 69622, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 3, 'selected': False, 'text': '<p><code>[0-9][0-9][0-9]</code>, <code>[0-9]{3}</code> or <code>\\d{3}</code> don\'t work because they also match "1234".</p>\n\n<p>So it depends on what the delimiter is.</p>\n\n<p>If it\'s in a variable, then you can do <code>^/[0-9]{3}/$</code>. If it\'s delimited by whitespace you could do <code>\\w+[0-9]{3}\\w+</code></p>\n'}, {'answer_id': 69624, 'author': 'decibel', 'author_id': 11116, 'author_profile': 'https://Stackoverflow.com/users/11116', 'pm_score': 0, 'selected': False, 'text': '<p>[0-9][0-9][0-9] will match a <em>minimum</em> of 3 numbers, so as Joe mentioned, you have to (at a minimum) include \\b or anything else that will delimit the numbers. Probably the most sure-fire method is:</p>\n\n<p>[^0-9][0-9][0-9][0-9][^0-9]</p>\n'}, {'answer_id': 69636, 'author': 'Lukas Šalkauskas', 'author_id': 5369, 'author_profile': 'https://Stackoverflow.com/users/5369', 'pm_score': 2, 'selected': False, 'text': '<p>You should use this: </p>\n\n<pre><code>"^\\d{3}$"\n</code></pre>\n'}, {'answer_id': 69644, 'author': 'Antti Rasinen', 'author_id': 8570, 'author_profile': 'https://Stackoverflow.com/users/8570', 'pm_score': 2, 'selected': False, 'text': '<p>As others point out, you need to match more than just the three digits. Before the digits you have to have either a line-start or something that is not a digit. If emacs supports \\D, use it. Otherwise use the set [^0-9].</p>\n\n<p>In a nutshell:</p>\n\n<pre><code>(^|\\D)\\d{3}(\\D|$)\n</code></pre>\n'}, {'answer_id': 78021, 'author': 'Chopmo', 'author_id': 13747, 'author_profile': 'https://Stackoverflow.com/users/13747', 'pm_score': 2, 'selected': False, 'text': '<p>When experimenting with regular expressions in Emacs, I find <strong>regex-tool</strong> quite useful: </p>\n\n<p><a href="ftp://ftp.newartisans.com/pub/emacs/regex-tool.el" rel="nofollow noreferrer">ftp://ftp.newartisans.com/pub/emacs/regex-tool.el</a></p>\n\n<p>Not an answer (the question is answered already), just a general tip. </p>\n'}]
['https://Stackoverflow.com/questions/69591', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6180/']