Thomas Tallaksen
Thomas Tallaksen

Reputation: 97

Reference regex when using find and replace in Notepad++

I use this regex ^.*?\K\d+\.\d+\.\d+ to search for the first date on every line in notepad++. It seems to work just fine.

The text I work with looks something like this:

Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017, Martin Lægland 31.12.2017 Publisert på nett.
Nå skal folk få fred, iTromsø, 09.09.2017 19:09 Martin Lægland, Publisert på nett.

The search gives me every 09.09.2017, but not 31.12.2017 on the second line. Which is exactly what I want. I then want to replace all the 09.09.2017s so they becomes *09.09.2017*.

How do I reference ^.*?\K\d+\.\d+\.\d+ in the replace field in notepad++? Earlier I have been able to do this with *\1* in the replace field, where \1 references the regex I've used, but that doesn't seem to work now.

Upvotes: 3

Views: 47

Answers (2)

pryashrma
pryashrma

Reputation: 102

you could use "$&" instead of \1

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626774

Your previous regex looked like \K(([0-9]{2}).([0-9]{2}).([0-9]{4})) with a capturing group set around the whole pattern. That is why \1 referred to the whole match that was also captured into Group 1.

You may either capture the whole pattern with (...) again and use *\1*, or use $0 backreference instead of \1:

^.*?\K\d+\.\d+\.\d+

replace with

*$0*

where $0 is the backreference to the whole match.

enter image description here

Upvotes: 2

Related Questions