1

I'm trying to use M-x replace-regex to find and replace the first space on each line. What regex would I use to do this?

Everything I've tried either finds no matches or every space.

The lines never start with a space.

As an example, if my line consisted of What did the fox say? I would like to replace the space between What and did. I would not like to match the What.

  • So even if a line doesn't start with a space, e.g. this is a line, you want to replace the space between this and a, correct? Or only if the line starts with a space? – elethan Mar 21 '16 at 21:30
  • None of the lines start with a space. In the example this is a line I would like to replace the space between this and is. – Gregory Arenius Mar 21 '16 at 21:45
6

You can use the regexp “^\([^ ]*\)”. Please note that it ends with a space. There is also a single space character inside [^ ]. Specify that one, and replace with “\1X” (where the X stands for whatever you want to replace the space with).

So this is what you type. Below, spaces are not to be typed in, but the string SPACE is to be typed as a single space character, and RET as a return. Ready? Here we go:

C-M-% ^\([^ SPACE ]*\) SPACE RET \1 RET

Note that there can be no regexp that matches the first space character on the line. Therefore, the above solution matches all text up to and including the first space, then remembers the leading part by putting it inside \(…\). The \1 in the replacement text will match this part, so the net effect is just to remove the space.

(Heavily edited to explain more fully.)

  • @HaraldHanche-Olsen I went ahead and replaced that last space in the regexp with \\s-+ for clarity. I believe that the OP missed noticing that trailing space and hence the above comment. Feel free to revert this edit. – Kaushal Modi Mar 22 '16 at 3:02
  • @KaushalModi \\s- means something different: Any character with whitespace syntax. I assumed the OP meant a literal space. If he did mean any whitespace character, the final space should be replaced by \\s- as you did, but then also [^ ] should be replaced by \\S-. – Harald Hanche-Olsen Mar 22 '16 at 9:55
0

This is the idea: capture all non-space characters from the beginning of the line in the regex in one set of parenthesis, and then the first space, replacing with the first capture plus something instead of the space.

Here is that shown as a function you can run, rather than a direct command (easier to read and test):

(defun asjo-test-rr ()
    (interactive)
    (replace-regexp "^\\([^ ]+\\)\\( \\)" "\\1ABBA"))

Now, if you evaluate that function (put your cursor after the last parenthesis and go C-c C-e), and then you go to a line, say your example:

What did the fox say?

and you type M-x asjo-test-rr, the result is:

WhatABBAdid the fox say?

i.e. the first and only the first space has been replaced (with "ABBA").

You can of course type in the regexp and replacement string in the M-% prompt, if you're not afraid of leaning toothpick syndrome.

(Although I've got to admit, I can't make it work doing that right now.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.