Search code examples
regextextmate

Strip first space to end of line


I am trying to strip the first space to the end of the line on each line of a text file in TextMate using the Find and Replace with Regular Expressions.

For example I want to replace the following line:

One, 2, Three!

With:

One,

I have looked at this question, but I don't really understand how to use the accepted answer. It seems to be matching the whole line, and not the first space onwards.

Do I need to use a lookbehind to match the first space?

I started by trying:

^([^\s]*)\s.+$

Which matches the whole line if there is a space, but it there are no spaces on the line it matches two lines at a time.

When I do have the correct regex, do I just put it in the "Find" box and hit "Replace All" with the "Replace" box blank? I also don't really understand the "replacement pattern" syntax the answerer is talking about in the question I linked to.

Thanks.

EDIT Aug 9, 2018: This seems to be getting some views. If anyone is looking to do this with command-line tools, you can try:

awk '{print $1}' myfile.txt > output.txt

or to write back to the same file you can do:

awk '{print $1}' myfile.txt | sponge myfile.txt

This will take the first column before any whitespace, including tabs. To split on spaces specifically:

cat myfile.txt | cut -d ' ' -f 1 > output.txt

Solution

  • There isn't one single correct regex. There's any number of ways to do this. You can use a lookbehind, but there's no need to. How to actually use the regex also depends on how the pattern is designed - you might have to replace with nothing, or you might have to replace with a fancy text that's more complicated than the regex pattern itself.


    Probably the easiest pattern to use is .*(?:\n|$) - replace with nothing. (Note the space in front of the dot.)

    Another one would be ^(.*?) .*?$, which needs the multi-line modifier to work. This one is trickier to use: you have to replace with \1. This replacement pattern references text that has been captured by the regex - in this case, all the text up to the first space. If you omitted this, all lines containing a space would be deleted.