Search code examples
regexnotepad++programmers-notepad

How to replace a particular character after '=' sign in all the rows in notepad++


My file content is as below

abcd-12=jksjd-jkkj
xyzm-87=hjahf-tyewg-iuoiurew
zaqw-99=poiuy-12hd-jh-12-kjhk-4rt45

I want to replace the hypen with underscore sign after the '=' i.e on the R.H.S of the equation.

No of hypenated terms are variable in the lines, it can be 3 or 4 or 5

How to do this for the entire document. Left hand side should be intact.

My desired result is :

abcd-12=jksjd_jkkj
xyzm-87=hjahf_tyewg_iuoiurew
zaqw-99=poiuy_12hd_jh_12_kjhk_4rt45

Solution

  • This will replace any number of hyphens in a single pass:

    • Ctrl+H
    • Find what: (?:=|(?!^)\G).*?\K-
    • Replace with: _
    • CHECK Wrap around
    • CHECK Regular expression
    • UNCHECK . matches newline
    • Replace all

    Explanation:

    (?:             # non capture group
        =           # equal sign
      |             # OR
        (?!^)       # negative lookahead, make sure we are not at the beginning of a line
        \G          # restart from last match position
    )               # end group
    .*?             # 0 or more any character but newline, not greedy
    \K              # forget all we have seen until this position
    -               # a hyphen
    

    Screen capture (before):

    enter image description here

    Screen capture (after):

    enter image description here