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

377 views Asked by At

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
3

There are 3 answers

3
Toto On BEST ANSWER

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

4
Tim Biegeleisen On

One option would be the following find and search, in regex mode:

Find:    = ([^-]+)-([^-]+)$
Replace: = $1_$2

Demo

The stategy here is to match and capture both halves of the hyphenated term, occurring on the RHS of the equation. Then, replace with those two halves separated by an underscore.

Edit:

If the RHS really has four hyphenated terms, then use:

Find:    = ([^-]+)-([^-]+)-([^-]+)-([^-]+)$
Replace: = $1_$2_$3_$4
2
virolino On

Search for:

(=[^-\r\n]+)-

replace with:

\1_

NOTE: search and replace repeatedly until no more replacements are made.

Test here.