Reputation: 1610
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
Upvotes: 2
Views: 621
Reputation: 91430
This will replace any number of hyphens in a single pass:
(?:=|(?!^)\G).*?\K-
_
. matches newline
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):
Screen capture (after):
Upvotes: 2
Reputation: 2201
Search for:
(=[^-\r\n]+)-
replace with:
\1_
NOTE: search and replace repeatedly until no more replacements are made.
Test here.
Upvotes: 0
Reputation: 521289
One option would be the following find and search, in regex mode:
Find: = ([^-]+)-([^-]+)$
Replace: = $1_$2
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
Upvotes: 3