I have 30,000 lines of text that have randon number of spaces in them. I need to add a character to just the first SPACE in each line. Any who can suggest anything would be greatly appreciated.

  • Please edit and provide before and after examples. A few lines for each should be sufficient.
    – DavidPostill
    Jun 11, 2016 at 8:59

5 Answers 5

  • Ctrl+H
  • Find what: ^\S+\h\K
  • Replace with: %
  • check Wrap around
  • check Regular expression
  • Replace all


^           # beginning of line
  \S+       # 1 or more non space
  \h        # 1 horizontal space
  \K        # forget all we've seen until this position


%           # the character to insert


I have 30,000 lines of text that have randon 
number of spaces in them. I need to add a character 
to just the first SPACE in each line. 
Any who can suggest anything would be 
greatly appreciated.

Result for given example:

I %have 30,000 lines of text that have randon 
number %of spaces in them. I need to add a character 
to %just the first SPACE in each line. 
Any %who can suggest anything would be 
greatly %appreciated.

enter image description here


Try TextPad. You can record macros and then play them back. So record what you want to do for the first line and after you have completed the first line, leave your cursor at the beginning of the next line. Then just play the macro until the end of the file. Or just hold down the play macro key.


  • Thanks, I did try this but the SPACE is not always in the same place on the line. Macro merely counts the movements before adding the character, it does not look for the SPACE in each line. Othe.rwise that would have worked fine Jun 11, 2016 at 3:06
  • You can search for the space in your macro. Do this simple by searching when you record your macro.
    – Dale
    Jun 11, 2016 at 3:07
  • Thanks Dale, That worked well. Just have to learn to think laterally now. -) Jun 11, 2016 at 3:11
  • Awesome. I've had to do that tons of times for cleaning data. Please mark my answer. :)
    – Dale
    Jun 11, 2016 at 3:12
  • Oh Dale check out my answer because if you've used a macro like that multiple times you may be interested in learning about the joy of regular expressions yourself.
    – Wolfkin
    Jun 13, 2016 at 16:22

What you're looking for is regular expressions. The first thing you're going to want to do is turn your search into regular expressions

enter image description here

Joy to be had because I think i know exactly how to solve this


The expression you want is:
Find: (^.*?)( )
Replace: \1#\2

The way this works is as follows in regular expressions the () means "capture". This means it will find this highly specific group and then you can refer to it in the replacement with \1 or \2 or \3. The number starts at 1 and you count from the open paranthesis

^ means start from the beginning of the line. (Because you want your insert to cover the FIRST space that's what we'll look for starting from the front find the first space)

.*? this part finds the first word in front of the space. It does this by making in one group any character (.) as many as possible (*) but the fewest necessary ?. Without the question mark you'll just capture every character (including all the spaces)

( ) this part means our second group is the space after the first one. This should be the first space. This is what we want to insert our character.

The substitution


\1 refers to the first characterset. And the * in the expression means that character set can be a word any length including 0.

# is the insert character

\2\ is of course the space.

The result is putting a # in front of every first space. If you want it AFTER the first space it's as simple as adjusting the substitution

\1\2# will put the # after every first space per line.

This is the power of regular expressions. A simple find and replace can do the insert more precisely than any macro. It's flexible it's fast and most of the time it's not that hard.

Here's a great starter tutorial that should explain what RegEx is and why it's useful. [RegExOne]. REO is the one that got me to understand

This is a tutorial that a lot of people recommend that honestly I couldn't make much sense of. [RegEx.Info]

I actually never went back to RegEx.info I jumped straight into Notepad++ Documentation which was actually at this point very helpful. That and of course you can test with [RegEx101]. The only things you'll want to do is add the /gm in the expression. Then you can just paste a snippet into the TestString and practice until you get it right.

enter image description here


FIND: ^(\w+)\K



There is a very simple solution for this.

I remembered there is something in Notepad++ already.

  • Open your Replace window
  • Put "^" in the Find field followed by a space or whatever you want to find
    This will in only search for anything at the start of your line
  • Put whatever you need in the Replace with field
  • Set Search Mode to "Regular expression"
  • Click Replace All
  • Done

For more information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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