I am trying to use regular expressions to remove all whitespaces after a specific word. The editor I am using is Notepad++ but am able to use Unix

In this case it is VALUES I'm able to highlight by running regex \VALUES but I am unsure how to remove whitespaces after this.

share|improve this question
5  
By "after", do you mean immediately after, or anywhere after? – Kusalananda 14 hours ago

Using sed:

$ sed 's/VALUES[[:blank:]]*/VALUES/g' input >output

This will replace VALUES followed by any number of whitespace characters (space or tab) with VALUES, i.e. it will remove the whitespace.

If it's important that VALUES is matched as a complete word, i.e. that whateverVALUES is not matched, then insert a (beginning-of-)word boundary pattern before VALUES:

$ sed 's/[[:<:]]VALUES[[:blank:]]*/VALUES/g' input >output

I will leave that out for the remainder of this answer.

For a more complicated value of VALUES, it might be convenient to not have to type it twice:

$ sed 's/\(VALUES\)[[:blank:]]*/\1/g' input >output

This saves the VALUES pattern and reuses it in the replacement.

If the pattern VALUES is stored in the shell variable $values:

$ sed "s/\($values\)[[:blank:]]*/\1/g" input >output

This transfers more or less directly into the Vim editor:

:%s/\(VALUES\)[[:blank:]]*/\1/

As I've never used Notepad++, I can only guess how to use it. One should apparently be able to press Ctrl+H and enter a search/find and replace pattern.

The search pattern may be (VALUES)[[:blank:]]* while the replace pattern may be $1. I have no way of testing this, sorry. If [[:blank:]] doesn't work, try with [\t ].

share|improve this answer
    
This will replace the "VALUES" with "VALUES" even if there is not space after it, unnecessary replacement – Mhd Wissam Al-Roujoulah 13 hours ago
4  
@MhdWissamAl-Roujoulah It will indeed. And it is unnecessary. But the extra time taken to figure out whether to use + or \+ in place for * is longer than the time taken to execute a few extra substitutions, and the end result will be identical. – Kusalananda 13 hours ago
2  
Also it might catch words ending in VALUES (though LVALUES is the only one I can thing of off the top of my head). – jamesqf 9 hours ago
    
@jamesqf Now that's a good point that I didn't think of! Come back in five minutes and I will have fixed that. – Kusalananda 9 hours ago

You can use \s* (GNU) or [[:space:]]* (portable) to catch tabs (and all other horizontal and vertical spacing characters) as well as spaces.

$ cat file
stuff and VALUES    <--tab
more VALUES   <--three spaces
VALUES      <--tab, three spaces, tab

$ sed 's/VALUES\s*/VALUES/' file
stuff and VALUES<--tab
more VALUES<--three spaces
VALUES<--tab, three spaces, tab
share|improve this answer
4  
Note that since \s or [[:space:]] match on the CR character, that could corrupt MS-DOS line delimiters. See [[:blank:]] for horizontal spacing characters only. – Stéphane Chazelas 14 hours ago

Using sed:

sed -e 's/VALUES \+/VALUES/'

Using + to match all spaces after VALUES

share|improve this answer

In Notepad++ bring up the find and replace window

In the find box, enter (VALUES)\s* (this matches "VALUES" followed by any amount of whitespace) In the replace box, enter \1 (this insterts the first group (inside parenthesis ())

Ensure regular expressions are enabled

Click replace all to replace all instances of "VALUES[some whitespace]" with "VALUES"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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