Author Topic: Selective replacements  (Read 1861 times)

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Selective replacements
« on: April 23, 2014, 06:38:45 pm »
I am trying to search for a bunch of print statements that I want to filter as follows:

1.   I want to select all dbg_printfs.
2.   Out of all of those I want to select those that have value.stringValue().
3.   Out of those I only want those that do not have value.stringValue().value().
4.   Finally, I want to replace int those lines 'value.stringValue()' with 'value.stringValue().value()'.

I looked at an old macro I had written for some other task to see if I could re-use it but it isn't easy for me to adapt it (I know very little of SE macro programming and haven't had time to practice). If there is a way to do this without a macro, let me know.

Code: [Select]
_command selective_replace() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   int not_found = 0; /* boolean to terminate loop */

   _str text = '_debug.printf'
   if (search(text, 'R') == 0) {
      // print line here
   }

   while (!not_found) {
      if (search(text, 'R') == STRING_NOT_FOUND_RC) {
         not_found = 1
      }
   }
}
« Last Edit: April 23, 2014, 06:41:53 pm by flethuseo »

hs2

  • Senior Community Member
  • Posts: 2735
  • Hero Points: 287
Re: Selective replacements
« Reply #1 on: April 23, 2014, 08:28:24 pm »
Sounds like this could be done w/o any extra macro power and just let the mighty SE regular expression engine doing its magic:
Try this on the SE commandline (for current document) :
Code: [Select]
c/{_debug.printf?+value.stringValue\(\)}~(.value\(\))/#0.value\(\)/rh#$You might append '*' to the options do it in one go.
or use the replace dialog using the (SE style) regexps of the command.

This was my test bench which was hopefully matching the case you described:
Code: [Select]
       _debug.printf("bla text");
       _debug.printf("bla ok %s", value.stringValue().value());
       _debug.printf("bla fix %s", value.stringValue());

Hint: Using the very convenient 'Regex evaluator' tool window saves you a LOT of time and pain when fiddeling around with regular expressions.

Hope it helps, HS2
« Last Edit: April 23, 2014, 08:31:17 pm by hs2 »

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Selective replacements
« Reply #2 on: April 24, 2014, 04:08:12 pm »
Thanks HS2, I usually write my Regular expressions in the Perl format. Do regex expressions in SE support more stuff?

hs2

  • Senior Community Member
  • Posts: 2735
  • Hero Points: 287
Re: Selective replacements
« Reply #3 on: April 24, 2014, 06:05:41 pm »
The diff. types of regexps are just a question of style/syntax. I guess there is only 1 underlying engine.
However, one of the very cool things of the SE search/replace feature is e.g. color coding sensitive operation.
On top of using regexps this could also help a lot.
HS2