30

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.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Alex B
  • 24,678
  • 14
  • 64
  • 87
  • When testing the answers below try M-x regexp-builder; it's very very helpful. – David Webb Sep 16 '08 at 08:27
  • If you use regexp-builder, keep in mind that you need to use one more backslash for each escape, since it is building an expression that you could embed in code, not one you would use at the (query-)replace-regexp prompt. – Joe Hildebrand Sep 16 '08 at 15:26

7 Answers7

57

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 \b to match word boundaries around the numbers. This leaves:

\b[0-9]\{3\}\b

For those wanting more information about \b, see the docs:

matches the empty string, but only at the beginning or end of a word. Thus, \bfoo\b matches any occurrence of foo as a separate word. \bballs?\b matches ball or balls as a separate word. \b matches at the beginning or end of the buffer regardless of what text appears next to it.

If you do want to use this regex from elisp code, as always, you must escape the backslashes one more time. For example:

(highlight-regexp "\\b[0-9]\\{3\\}\\b")
Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48
11

[0-9][0-9][0-9], [0-9]{3} or \d{3} don't work because they also match "1234".

So it depends on what the delimiter is.

If it's in a variable, then you can do ^/[0-9]{3}/$. If it's delimited by whitespace you could do \w+[0-9]{3}\w+

SCdF
  • 57,260
  • 24
  • 77
  • 113
4

You should use this:

"^\d{3}$"
Lukas Šalkauskas
  • 14,191
  • 20
  • 61
  • 77
3

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].

In a nutshell:

(^|\D)\d{3}(\D|$)
Antti Rasinen
  • 9,918
  • 2
  • 22
  • 18
  • \b catches the (^|\D) and (\D|$) a lot more easily. It won't work when the thing you're matching on is not a word character, but \B often works in those cases. – Joe Hildebrand Sep 16 '08 at 06:20
2

When experimenting with regular expressions in Emacs, I find regex-tool quite useful:

ftp://ftp.newartisans.com/pub/emacs/regex-tool.el

Not an answer (the question is answered already), just a general tip.

Chopmo
  • 1,416
  • 1
  • 10
  • 18
0

[0-9][0-9][0-9] will match a minimum 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:

[^0-9][0-9][0-9][0-9][^0-9]

decibel
  • 421
  • 3
  • 6
  • As Antti implied, this approach suffers from not matching at the beginning or end of the file/line (depending on options). \b catches those. – Joe Hildebrand Sep 16 '08 at 06:19
-3

It's pretty simple:

[0-9][0-9][0-9]
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
mike511
  • 1,685
  • 4
  • 16
  • 18