Ken
Ken

Reputation: 1021

Notepad++: Search and Replace with Regular Expression

I have thousands of entries in Notepad++ (amongst other text mixed in), where I'm trying to remove the dash symbol in-between any 2 numbers.

I have data like this:

text text this is text here
234-5678
this is more text here 
123-4585 this is more text here
or text is here too 583-5974

Desired Results:

text text this is text here
2345678
this is more text here 
1234585 this is more text here
or text is here too 5835974

I've tried:

Find: [0-9]-[0-9] (which works to find the (#)(dash)(#)
Replace: $0, $1, \1, ^$0, "$0", etc.

Upvotes: 0

Views: 1873

Answers (2)

Jerry
Jerry

Reputation: 71538

What's wrong with your method of:

Find: [0-9]-[0-9] (which works to find the (#)(dash)(#)
Replace: $0, $1, \1, ^$0, "$0", etc.

Is that $0 or $1 etc refer to captured groups while your find regex doesn't have any. It would have worked if you did:

Find: ([0-9])-([0-9])
Replace: $1$2

$1 would contain the first digit and $2 the second.

Of course now, using lookarounds like in edi_allen's answer ((?<= ... ) is a positive lookbehind and (?= ... ) is a positive lookahead) also work and avoids you the trouble of using backreferences (the $1 and $2).

$0 contains the whole match. $1 contains the first captured group and $2 contains the second captured group.

Upvotes: 2

edi_allen
edi_allen

Reputation: 1872

Try this regex.

(?<=\d)-(?=\d)

Upvotes: 3

Related Questions