Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace across several files using regular expressions in Qt Creator

I am writing a C++ library and have just remembered that I need to export each class.

The following code:

class MyClass

should become:

class MY_EXPORT MyClass

I have several classes that need this change - is there a way to do this using Qt Creator?

like image 503
Mitch Avatar asked Feb 11 '23 09:02

Mitch


1 Answers

You can achieve this using the Advanced Search feature:

  1. Edit > Find/Replace > Advanced Find > Open Advanced Find... or CTRL + SHIFT + F
  2. In the Search Results window that opens, select an appropriate Scope (Current Project is probably sufficient in your case).
  3. Check Use regular expressions.

  4. In the Search for field, type:

    ^class (.*[^;])$

    This searches for all class declarations that begin at the start of the line and don't end with a semicolon (to exclude forward declarations).

  5. In the File pattern field, type:

    *.h

    This will ensure the search only happens within header files.

  6. Click the Search & Replace button. You'll be presented with a list of search results, and the message "This change cannot be undone." - now would be a good time to commit your work if you haven't already.

  7. Expand each search result to check that the matches are correct. Once you're satisfied, type the following into the Replace with field:

    class MY_EXPORT \1

    This adds MY_EXPORT before the name of each class, using a numbered backreference to insert the class name captured with the group back in step 4.

  8. Click the Replace button, and you're done.

like image 196
Mitch Avatar answered Feb 13 '23 02:02

Mitch