Ke.
Ke.

Reputation: 2586

notepad++ regex replace word in first line

Im trying to use the following regex to search and replace in multiple files in notepad++

([^\n]*)(state="1")([^\n]*)*.

This searches and finds state="1" in the first line of each file and works fine.

However, when I try to replace state="1" using:

Replace with: $1 state="5"

it cuts off the rest of the line.

I thought that it might be possible to get the rest of the line using:

Replace with: $1 state="5" $2

However, $2 doesnt seem to exist as a variable.

Is there some way to attach the rest of the line into variable $2?

Cheers

Heres an image to show how (?=\A[^\n]*)state="1" is not working

Ive updated my version of notepad++ and everything notworking

Upvotes: 2

Views: 2617

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 148980

Each capture group, (…), is assigned a number, so $2 represents the second capture group, (state="1"). The remainder of the line is captured in $3.

Either remove the capture group around state="1":

([^\n]*)state="1"([^\n]*)*.

Or use $3:

Replace with: $1 state="5" $3

Also, given the simplicity of the task, I don't see why you couldn't just search for state="1" and replace with state="5". There doesn't seem to be any need for regular expressions here.


Update There's nothing in the pattern listed so far which limits the result to only matching strings on the first line. If you need that I'd recommend using a pattern like this:

(?=\A[^\n]*)state="1"

With these settings:

screenshot


Update There seems to be some strange behavior with the \A (beginning of text) anchor inside the lookbehind. Removing from the lookbehind seems to work. Try this pattern:

\A([^\n]*)state="1"

And replace with:

$1state="5"

All the other settings should be fine.

Upvotes: 3

Related Questions