I want to create a regexp in Emacs that matches exactly 3 digits. For example, I want to match the following:
123 345 789
But not
1234 12 12 23
If I use [0-9]+ I match any single string of digits. I thought [0-9]{3} would work, but when tested in re-builder it doesn’t match anything.
If you’re entering the regex interactively, and want to use
{3}, 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\bto match word boundaries around the numbers. This leaves:For those wanting more information about
\b, see the docs:If you do want to use this regex from elisp code, as always, you must escape the backslashes one more time. For example: