PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Notepad++ regex to remove spaces between specific characters but not ALL spaces

I thought my regex skills were strong, but I'm getting crushed.

I have several lines in thsi format

Time: 105 0 0
Time: 88  0 1
Time: 44  1 1
Time: 64  1 0

I want theses to turn into this:

Time: 105 thread00
Time: 88  thread01
Time: 44  thread11
Time: 64  thread10

I can match the [0-9][ ][0-9] section... I match it with that regex right there!

But I don't know how to preserve the values AND remove the space. Replacing it wholesale with new stuff, sure... but how do I PRESERVE values?

Upvotes: 2

Views: 9415

Answers (2)

cr0ybot
cr0ybot

Reputation: 4090

Find what: (\d)\s(\d)$

Replace with: thread\1\2

\d matches any digit, \s matches any space character.

The parentheses will be captured for use as \1, \2, \3... and \0 will provide the entire match.*

$ matches the end of a line, so that you don't accidentally match the "5 0" in the first line.

*Note that some regex engines use the \1 pattern while some others will use $1. Notepad++ uses the former.

Upvotes: 3

scrowler
scrowler

Reputation: 24435

You can try this:

Pattern:

/^(.*)(\d+)\s(\d+)$/

Breakdown:

^       # start of line
(.*)    # the first part of the line      -- capture $1
(\d+)   # the first number (1 or more)    -- capture $2
\s      # the space between the numbers
(\d+)   # the second number (1 or more)   -- capture $3
$       # end of line

Replace:

/$1thread$2$3/

Result:

Time: 105 thread00
Time: 88  thread01
Time: 44  thread11
Time: 64  thread10

Demo: http://regex101.com/r/gB8uS4

Upvotes: 3

Related Questions