Author Topic: Multiple item Search & Replace ?  (Read 7655 times)

billprew

  • Community Member
  • Posts: 11
  • Hero Points: 0
Multiple item Search & Replace ?
« on: May 02, 2016, 05:38:10 PM »
I know that I can use REGEX to do a search and specify multiple search terms, like FOO|BAR and if I do a search the either FOO or BAR matches will be found.  It there anyway to do something similar in a Replace operation, but have different replacements for each matched term. So I want to replace FOO with AAA and replace BAR with BBB.  I tried using AAA|BBB as the replacement string, but that replaces either of the matching items with that full string, not what I want.  Wondering if there is a way to do this in Slickedit?

Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
Re: Multiple item Search & Replace ?
« Reply #1 on: May 02, 2016, 05:54:44 PM »
Search and replace twice?

billprew

  • Community Member
  • Posts: 11
  • Hero Points: 0
Re: Multiple item Search & Replace ?
« Reply #2 on: May 02, 2016, 05:57:53 PM »
Naturally, multiple manual single term search and replaces could be done, but that's harder to do and harder to replace if needed again in the future.  Was hoping there might be a way to do this in SLickEdit.  A friend recently showed how easy this is in Notepad++, and when I went looking for how I could do it in my preferred editor (SlickEdit) I couldn't find a way to do it documented.

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: Multiple item Search & Replace ?
« Reply #3 on: May 02, 2016, 06:14:25 PM »
Out of curiosity, how is that done in Notepad++?  I have never seen that in Notepad++, is that a plugin or add-on of some sort?  It sounds like an interesting feature, but I am not sure how it goes about handling nested or complex alternations.

billprew

  • Community Member
  • Posts: 11
  • Hero Points: 0
Re: Multiple item Search & Replace ?
« Reply #4 on: May 02, 2016, 07:16:39 PM »
I don't know how it would go for complex stuff, but for basic string replacements it worked okay.  I started with this text in a test file:

Lorem ipsum dolor sit amet, quod indoctum et vel, nam partem recteque petentium an.
Cum elitr everti ne, ea harum repudiare nec.
Vel ei dolor adipisci accommodare, mel duis velit audire cu.
Dicunt pertinax in mel. At movet simul ludus per.
Timeam oporteat ut vis.
At malorum democritum vituperata cum, esse indoctum ea quo.


Then did the attached image in a Replace dialog.

And got this result:

Lorem ipsCCC dolor sit amet, quod indoctCCC et vel, nam partem recteque petentiCCC an.
BBBm elitr everti ne, ea harCCC repudiare nec.
Vel ei dolor adipisci accommodare, mel duis velit audire BBB.
DiBBBnt pertinax in mel. AAA movet simul ludus per.
Timeam oporteAAA ut vis.
AAA malorCCC democritCCC vituperAAAa BBBm, esse indoctCCC ea quo.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #5 on: May 03, 2016, 06:58:44 PM »
Interesting. It inserts the string if the match group if it exists. Not hard to implement. The sytax is a poor choice because ( becomes an additional special character in the replace string.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #6 on: May 03, 2016, 07:03:46 PM »
I think a syntax like $(1X), $(<name>X),$('name'X),$({name}X) would make more sense. $ is already a special character in the replace string so no additional special characters would be added. The only downside with this syntax is that specifying a match group in the search string is different than the replace string. Any votes on this? What poison/syntax do you prefer?


$(...) for Perl #(...) for SlickEdit syntax of course.
« Last Edit: May 04, 2016, 01:33:56 AM by Clark »

billprew

  • Community Member
  • Posts: 11
  • Hero Points: 0
Re: Multiple item Search & Replace ?
« Reply #7 on: May 04, 2016, 11:31:20 AM »
I'm not a regex or Perl jock, so I'm the wrong person probably to provide much feedback on the syntax.  Would this 'feature' only be available in regex mode, not plain text search / replaces?  That seems to make sense, since we are using some regex type syntax in the search field, but I'm sure there will be people that would want it in plain text mode too.  I guess the only way to accomplish that though is to have another dialog box with the search and replace pairs on individual lines.  That feels like overkill here.

It would be good to stay as close to regex syntax as possible I guess, I know it has some ability to refer back to searched groups, but I'm not a master of that syntax.

Another approach I suppose would be to "imitate" the format of the search string.  So if in multi-mode, and the search string is "(aaa)|(bbb)|(ccc)" then the replace string would be "(111)|(222)|(333)", but if the search string was "{aaa}|{bbb}|{ccc}" then the replace string would be "{111}|{222}|{333}".  You get the idea.  Takes it further away from base regex in the search side though, but makes the replace format clearer.

All just brainstorming, thanks for the follow up.

~bill

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #8 on: May 04, 2016, 04:41:05 PM »
I don't understand the syntax you describe since you need to specify a match group name or index and what gets inserted if that match group exists. Would the  stuff that gets inserted follow the match group index (ex. (111X)|(222Y)|(333Z))?

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Multiple item Search & Replace ?
« Reply #9 on: May 04, 2016, 05:06:28 PM »
Don't expose the syntax - make Slick handle it internally.
Well, for scripting I suppose you need to have some sort of syntax - or varargs, or arrays of parameters --- but RegEx's are complicated enough for the programmer to craft and later interpret - without adding a whole new dimension.



Allow multiple independent search / replace strings, each can be enabled / disabled.
In the screen shot I attached, they are static, but better would be as a variable length list.


Actually ... the list could even be the same as search/replace history - so if I do 5 s/r operations on one file, I could go to the next file and do all 5 at once - instead of trying to apply them serially.


This would be a *fantastic* feature - I've so often wanted this.




Tim Kemp

  • Senior Community Member
  • Posts: 546
  • Hero Points: 92
Re: Multiple item Search & Replace ?
« Reply #10 on: May 04, 2016, 06:09:34 PM »
This seems like an infrequent, specialized operation. The capability to do this already exists. This is the exact type of thing macros are great for.

I have had reason to do search and replace on a bunch of items at a time. I generated a macro by recording a search and replace. Then I edited the macro and duplicated the _mfreplace2(). That way I didn't have to remember command or type out all the arguments. I had dozens of similar strings to replace. Doing this manually would have taken me hours and I would have screwed it up. I know, because the first few times I tried, it did take me hours and I did screw it up. Once I made the macro it all happened in seconds and there weren't any mistakes.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #11 on: May 04, 2016, 06:28:25 PM »
Good point. It's definitely not needed very often. For this reason, making ( a new special character is a bad idea or adding any new special character in the replace string that must be escaped to get the literal character is a very bad idea. Thanks for pointing this out.

The posted example isn't a good one though. It's too simple. I've had cases where doing multiple replaces doesn't work out so well. In fact, I've even had to do 3 replaces where the first replace is changing something to a temporary name so I can do the subsequent replaces. If I had a little more power, I could do this in one shot.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #12 on: May 06, 2016, 12:12:52 AM »
I've gone with the following syntax for v21:

Code: [Select]
$(<n1>X1|<n2>X2...|<nN>XN [|Xdefault] )
 

But there's more.... n1,n2,..nN can use & and | for logical AND and logical OR.

Code: [Select]
Examples:
  replace: (a)|(b)|(c)  with: $(<1>x)$(<2>y)$(<3>z)   a->x b->y c->z
  replace: (a)|(b)|(c)  with: ($(<1>x|<2>y|<3>z))   a->(x) b->(y) c->(z)
  replace: (?:(a)|(b))(?:(c)|(d))  with: $(<1&3>x|<2&4>y|z)   ac->x bd->y ad->z bc->z

billprew

  • Community Member
  • Posts: 11
  • Hero Points: 0
Re: Multiple item Search & Replace ?
« Reply #13 on: May 06, 2016, 01:57:48 PM »
I don't understand the syntax you describe since you need to specify a match group name or index and what gets inserted if that match group exists. Would the  stuff that gets inserted follow the match group index (ex. (111X)|(222Y)|(333Z))?
I was inferring the index by position, not data in the string.  So if found text matched the first search group, then the first replace group would be used to substitute.  Based purely on position in the search and replace strings, not on any ordinal indicators in the strings.  That make sense?

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6850
  • Hero Points: 526
Re: Multiple item Search & Replace ?
« Reply #14 on: May 06, 2016, 02:55:52 PM »
I don't understand the syntax you describe since you need to specify a match group name or index and what gets inserted if that match group exists. Would the  stuff that gets inserted follow the match group index (ex. (111X)|(222Y)|(333Z))?
I was inferring the index by position, not data in the string.  So if found text matched the first search group, then the first replace group would be used to substitute.  Based purely on position in the search and replace strings, not on any ordinal indicators in the strings.  That make sense?

That's does make sense. Thanks.