Apply regex to certain occurrences

4

I have a large file with plain text (no html tags, etc.). What I want to do is to apply regex to whole file on the one hand but on the other I want to choose which changes will be applied.

For example if I have a regexp like /house/ -> home I want to see some sort of preview and decide to which occurrences it will be applied, for ex. first and third.

What I need is editor which displays not only the preview of changes (something like powergrep editor (powergrep.com))but allow me to choose which changes to apply within. Let me give you a brief example. I want to change word Alice in whole book "Alice in wonderland" expcept first occurrence on page 55. Sure I can do the replace all and then change this one word on page 55 but that's not what I want.

piotr.gradzinski

Posted 2013-03-03T18:45:02.363

Reputation: 59

Answers

2

Well, if you're OK with vim, you can do an interactive search and replace like so

:%s/house/home/gc

This will prompt you before applying the regex substitution to each occurrence.

Joseph R.

Posted 2013-03-03T18:45:02.363

Reputation: 474

1

The Netbeans IDE has this functionality - regex replace with preview. Though it seems a little overkill to use such a heavy thing here..

Update

The functionality is called Replace in Projects. Go to Edit -> Replace in Projects or press CTRL + SHIFT + H.
Replace in Projects View
Select Browse in Scope section - it will let you browse for files you want. Now press Find. You will get the preview:
Replace preview
Choose which occurences to replace and press Replace X matches.

Shimon Rachlenko

Posted 2013-03-03T18:45:02.363

Reputation: 161

Yes, you can use regex replace with preview (highlighting results) but you can only do replace or replace all. You can't choose which replacement you want to do. As I said before. It's not a problem in 10 substitutions when you have to click 10 times. In 1000 substitution it matters. – piotr.gradzinski – 2013-03-03T20:45:23.573

1@dlm in last versions of Netbeans you can choose which of occurenses to substitute. I use Netbeans for development and this is one of features which make my life easier.. – Shimon Rachlenko – 2013-03-03T21:37:43.767

thank you. Can you describe how to do this? How to select the occurence you want to substitute? Using next/prev buttons? Some screenshots would be usefull. – piotr.gradzinski – 2013-03-03T21:46:33.557

I updated the answer with instructions how to do it. Hope it is of some help.. – Shimon Rachlenko – 2013-03-03T22:21:16.503

0

If you're not comfortable with vim ;), you can try for example TED Notepad (my favorite, neat but lots of features), Programmer's Notepad (both for Windows) or kate (Linux/KDE). However, I don't think there's a preview of the change...

mpy

Posted 2013-03-03T18:45:02.363

Reputation: 20 866

Well, actually the same thing I can do in eclipse or notepad++ or whatever. Youy have to hit enter for substitution. If you have 10 000 occurences then it's kind of a problem. If we keep in mind that we want to do first 5 000 substitutions ignoring 5 001 and then do 4 999 another so I would be nice to unclick this one substitution and then hit "replace all". Preview and unticking unwanted substitutions is crucial. – piotr.gradzinski – 2013-03-03T19:46:28.520

Ok, that was not clear to me from your question, perhaps you should clarify that point as you cannot take for granted everybody knows this powergrep. (Btw. why don't you use that editor if it fulfills your needs?) And please clarify if you are looking for a solution with Win, Un*x etc. – mpy – 2013-03-03T19:52:42.783

I'm very sory, it's my mistake. Powergrep does a lot of things (like regexp substitution preview) but not allows me to choose which occurence should be replaced and which not. I've edited my question. – piotr.gradzinski – 2013-03-03T20:00:18.293

I'm looking for a solution for Windows or Linux. – piotr.gradzinski – 2013-03-03T20:02:27.260

0

Mandatory emacs plug: In emacs (or xemacs, personal favorite) you can run query-replace-regexp, which will prompt for a regexp and replacement, and asks for each instance. Bound to M-C-% by default here. There is a decent xemacs version for Windows too.

vonbrand

Posted 2013-03-03T18:45:02.363

Reputation: 2 083