0

where ### is a three or four digit number followed by a space and I want to replace it with the same number followed by a space and a period.

I am using the regex search & replace in Open Office and I tried

"[0-9][0-9][0-9] "

which found the numbers I was looking for

replace with

$&

or

"[0-9][0-9][0-9]. "

neither worked

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Fred Zimmerman
  • 1,178
  • 3
  • 13
  • 29
  • 1
    Something along the lines of `s/([0-9]{3,4}) /\1\. /g` would do it, but, what @FatalError said... – imm Jul 07 '12 at 14:22

2 Answers2

1

In most regex implementations, "(\D\d{3,4}) " selects a 3 or 4 digit number with a nondigit prefix (otherwise it would find the 2345 in 12345). This way in ">1234 <" it will find ">1234 ", and put it in $1 or \1. So:

  s/(\D\d{3,4}) /\1. /g

Otherwise instead of \D you can use a word boundary, \< or \b or < depending on the dialect.

Edit: if \d is not supported, as in (apparently) my command line sed, even if I could have sworn to the contrary, use [0-9] to replace \d:

 sed -e 's/\([^0-9][0-9]\{3,4\}\) /\1. /g'

Test:

 $ echo "This is 12345 and 1234 and 123 and 12 and 1. 123. 1234." \
   | sed -e 's/\([^0-9][0-9]\{3,4\}\) /\1. /g'

Result:

 This is 12345 and 1234. and 123. and 12 and 1. 123. 1234.
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • 1
    `\D*` won't work anyway. If there happen to be any non-digits before the number, `\D*` will match them, but it doesn't *prevent* anything from matching there--even digits. – Alan Moore Jul 07 '12 at 16:25
  • True. I had removed the asterisk but did not update the first half of the answer. +Thanks for pointing that out. – LSerni Jul 08 '12 at 21:51
0

This appears to work in OpenOffice 3.3:

Search: ([^0-9][0-9]{3,4}) (with trailing space)

Replace: $1. (with trailing space)

Unfortunately, it won't match a 3- or 4-digit number at the start of the text.

MRAB
  • 20,356
  • 6
  • 40
  • 33