Skip to content

QtCreator – Search & Replace with Captured Texts

I wanted to replace many occurrences of

g_theme.themeDisplay(100, 80, 65)

by

g_theme.thSz([100, 80, 65])

in many QML files. The values of the three integer arguments varied from occurrence to occurrence. What I hoped for was that QtCreator’s search & replace function for regular expressions would support captured texts – the three arguments.

I entered

g_theme.themeDisplay\(([^\)]+)\)

in the field “Search for” of QtCreator’s search function, ticked “Use regular expressions”, and hit “Search & replace”. In the entry field for the replacement string, I entered

g_theme.thSz([\1])

and hit the “Replace” button. And – all was magically done!

The regular expression of the search term captures the text inside the parentheses of the function call with the subexpression ([^\)]+). The unescaped parentheses mark the text to be captured. The captured text is just everything that does not match a closing parenthesis – that is everything between the original opening and closing parenthesis.

The replace term accesses this captured text with \1. It’s all exactly as you would expect it from the QRegExp class.

Leave a Reply

Your email address will not be published. Required fields are marked *