Bob Smithsfield
Bob Smithsfield

Reputation: 35

substring replacement regex

Add a semicolon (;) to the end of every line.

This is what I have: ([^\n])$

The problem is it takes the last characther out instead of adding the semicolon on the end.

Also, I am using Notepad++

Upvotes: 0

Views: 362

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

Try this:

find: \r\n
replace: ;\r\n

In windows files, the common newline is CRLF (\r\n)

or

find: (.)(\r\n|$)
replace: $1;$2

to avoid to put ; in blank lines and to put it on the last line

Upvotes: 0

Martin Ender
Martin Ender

Reputation: 44259

I am assuming here your pattern is intentional, in that you only want to match line-endings of non-empty lines. (If you want to match all lines, simply remove the ([^\n]))

In the Replace with field you need to write back the match character:

$1;

Obviously, your match is replaced, so if you match a character before the end of line, it's going to be replaced. But if you wrap it in parentheses (like you did), it's captured and can be referred to in the replacement string.

Alternatively, you can use a lookbehind, which checks the condition but does not include its contents in the match:

(?<=[^\n])$

This way, you can simply replace with ;. In your case it doesn't really make a difference. In performance-critical scripts with more complicated expressions, capturing tends to be an expensive operation, so that lookarounds are often favorable.

Upvotes: 1

Related Questions