2

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'   
Vicky
  • 16,679
  • 54
  • 139
  • 232
  • Which version of TP are you using, which OS, and what RE flavor are you using in TP? – Kennah Oct 15 '15 at 20:56
  • @Kennah: Textpad 4.7.3 on Windows 7.. how to check the RE flavor on TP ? – Vicky Oct 16 '15 at 14:15
  • TP7, and I think TP6, use the ECMA standard, so there's no choice of RE flavor. However, with TP4.x, you definitely get a choice. I know it's in Configure, Preferences, but from there I cannot recall. I want to say that it's under Editor, but I cannot swear to it. – Kennah Oct 20 '15 at 17:03

2 Answers2

1

Search using this regx:

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

And replace using:

$1,0,$2

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

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • It works in the demo.. however, in Textpad, its giving -- Cannot find regular expression: '(.*?'hello'.*?),'hello',(.*)' – Vicky Oct 14 '15 at 22:28
  • Sorry working on Mac today so cannot test it on Textpad. But I would assume regex is same considering I didn't use any advanced feature like lookahead etc – anubhava Oct 14 '15 at 22:30
0

I voted up anubhava's solution since that put me very close to the solution.

Find this:

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

Replace with this:

$1,0,
Kennah
  • 487
  • 1
  • 5
  • 16