Search code examples
regextextpad

Regular expression to replace nth occurrence of a word on every line in Textpad


I have a file with thousands of lines containing comma separated columns.

'one',2,'three','hello','xyz',5,'hello','mnr','hello','axi'
'onae',2,'tree','hello','xyz',6,'hello','mnr','hello','asd'
'onee',2,'xdsa','hello','xyz',5,'hello','mnr','hello','aew'
'owne',2,'thr','hello','xyz',3,'hello','mnr','hello','az'
'ocne',2,'tee','hello','xyz',5,'hello','mnr','hello','zse'
'owne',2,'tre','hello','xyz',2,'hello','mnr','hello','aai'

Three of the columns in each line contains value as word 'hello'.

How can I replace the 2nd occurrence of word 'hello' with number 0 in every line using regex in Textpad such that the lines become:

'one',2,'three','hello','xyz',5,0,'mnr','hello','axi'
'onae',2,'tree','hello','xyz',6,0,'mnr','hello','asd'
'onee',2,'xdsa','hello','xyz',5,0,'mnr','hello','aew'
'owne',2,'thr','hello','xyz',3,0,'mnr','hello','az'
'ocne',2,'tee','hello','xyz',5,0,'mnr','hello','zse'
'owne',2,'tre','hello','xyz',2,0,'mnr','hello','aai'   

Solution

  • Search using this regx:

    (.*?'hello'.*?),'hello',(.*)
    

    And replace using:

    $1,0,$2
    

    Make sure DOTALL (dot matches newline) option is turned off.

    RegEx Demo