Home > other >  Replace the comma between two selected words using regex replacement
Replace the comma between two selected words using regex replacement

Time:08-08

I want to replace the comma (,) between two selected words. here is the input

test normal content [ABC]new, ball, test[/ABC]

output test normal content [ABC]new[/ABC][ABC] ball[/ABC][ABC] test[/ABC]

I actually want to add prefix and suffix. currently, I using

(?i)\[ABC\](. ?), (. ?)\[/ABC\]
<div >$1</a> <div >$2</a>

but in this way, i have to add a rule for each number of words. so i thought if i can replace the comma with [/ABC][ABC] and then I can simply use regular replace with [/ABC] and [ABC] to add prefixes and suffixes. it's a little bit complicated for me to write regex replace for such a problem. any help or guidance will be appreciated.

CodePudding user response:

  • Ctrl H
  • Find what: (?:\[ABC]|\G)[^[,] \K,(?=.*\[/ABC])
  • Replace with: [/ABC][ABC]
  • TICK Wrap around
  • SELECT Regular expression
  • Replace all

Explanation:

(?:         # non capture group
    \[ABC]      # literally [ABC]
  |           # OR
    \G          # restart from last match position
)           # end group
[^[,]       # 1 or more any character that is not square bracket or comma
\K          # forget all we have seen until this position
,           # a comma
(?=         # positive lookahead, make sure we have after:
    .*          # 0 or more any character
    \[/ABC]     # literally [/ABC]
)

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

  • Related