2

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
Som
  • 1,522
  • 1
  • 15
  • 48

3 Answers3

3

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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • U r great !!! Thank you Man .. I am going to accept your answer .. It tells me to wait for another 5 mins... However I have one more problem.. I have multiple hypenated text in the RHS. Your solution is replacing the first match and ignoring the rest. Below is my input : abcd-12=jksjd-jkkj-kjk-766 xyzm-87=hjahf-tyewg-766-gffg zaqw-99=poiuy-12hd-0909-iuy – Som Nov 11 '19 at 12:56
  • 1
    @Som If the number of hyphenated terms be really variable (e.g. it could be, or 3, or 4), then this could tricky in NPP. I updated my answer with an option for 4 terms. – Tim Biegeleisen Nov 11 '19 at 12:58
  • Your answer is working perfectly, if there are 4 or less no of hypenated terms. So we need to check what is the max no of hypenated term and give a count more than that. I think it will work that way. – Som Nov 11 '19 at 13:13
  • Are you open to something like Python? It would be much easier to handle there. – Tim Biegeleisen Nov 11 '19 at 13:14
2

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

Toto
  • 89,455
  • 62
  • 89
  • 125
0

Search for:

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

replace with:

\1_

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

Test here.

virolino
  • 2,073
  • 5
  • 21
  • will this replace multiple hypenated terms on the R.H.S ? – Som Nov 11 '19 at 13:19
  • with the updated regex, it does. Initially, it made some extra replacements on the L.H.S. It will replace any number of dashes, you just need to keep replacing until everything is replaced. I just tested it in NPPP. – virolino Nov 11 '19 at 13:28