Search code examples
notepad++

Notepad++ how to find and replace partially within single line


i want to replace only specific part of the line.

Below is the example:

OPPO|||CPH1931|||Jl. ABC No.1,A,E
OPPO|||CPH1931|||Jl. DEF No.2,X,Y
OPPO|||CPH1931|||JL. GHI No.3,Z,A

And below is the result that i wanted. Notice there is a double quotation mark "" after the last |||.

OPPO|||CPH1931|||"Jl. ABC No.1,A,E"
OPPO|||CPH1931|||"Jl. DEF No.2,X,Y"
OPPO|||CPH1931|||"JL. GHI No.3,Z,A"

Is it possible to do this with RegEx? If yes, how would i do that?

Thanks before.


Solution

  • You can use the fact that .* is greedy and will not match a newline character (by default). So:

    Find what: (.*\|\|\|)(.*)
    Replace with: \1"\2"
    ⦿ Regular expression     ▢ . matches newline (not checked!)

    Replace All

    Explanation

    • .* greedily matches any character (except newlines) any number of times.
    • .*\|\|\| matches as many characters as possible until an occurrence of |||. As .* is greedy, this means the last ||| occurrence on the current line will be included here. Any previous occurrences of ||| on the same line will already have been absorbed in the .* part.
    • (.*\|\|\|) puts that match in a capture group so later it can be referenced with \1.
    • (.*) will match any remaining characters on the same line in a second capture group, which can be referenced with \2
    • \1"\2" will reproduce the two capture groups, and adds literal double-quotes around what was captured in the second capture group.