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.

4 Answers 4

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.

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.

4
  • 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. Mar 3, 2013 at 20:45
  • 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.. Mar 3, 2013 at 21:37
  • 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. Mar 3, 2013 at 21:46
  • I updated the answer with instructions how to do it. Hope it is of some help.. Mar 3, 2013 at 22:21
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...

4
  • 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. Mar 3, 2013 at 19:46
  • 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
    Mar 3, 2013 at 19:52
  • 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. Mar 3, 2013 at 20:00
  • I'm looking for a solution for Windows or Linux. Mar 3, 2013 at 20:02
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .