Removing the T() Macro with a RegEx

I had to do some exploring and experiments, so maybe this will help others.

To remove the juce T() macro can be a huge pain manually, and the best way seems to be a RegEx (regular expression). These notes are specific to Xcode, but it will be similar in any text editor that supports RegEx.

You’re going to search for:T\("(.*?)"\)
And replace with"\1"
In the search, you have:

T(" - this searches for T(" - the paranthesis open is escaped, since it’s a special character in a regex.
b [/b] - this looks for any string, with . being any single character, and * being any number of them. The ? makes it non-greedy, so the first match of the next chunk will stop it. Without the ?, a line with multiple T() macros would be in one result together. The b[/b] parantheses surrounding the pattern makes regex ‘store’ the found characters as a sort of variable. We can use that in the replace.
") - this matches the end of a macro, with the paranthesis escaped, as before.

Replace is simply - “\1” - where the quote marks are literal, and \1 means 'the first variable you found in parantheses in the find operation. I understand some systems expect $1 instead, FYI.

It’s not perfect - I’m sure there’s patterns within a legal string that would cause a false positive, but it’s the simplest workable version.

To use in xcode, use the find in project dialog, set it to regular expression, and disable ignore case, so splat(“etc”) type strings don’t get caught. I recommend you use the ‘Selected Project Items’ choice, and in the left hand list of groups in your project, select only groups containing your files, not external files or text files.

Hope that helps!


Excellent tip! Thanks Bruce!

Just came across this quote:

Some people, when confronted with a problem, think
“I know, I’ll use regular expressions.” Now they have two problems.


Thanks for the advice, I just tried this in Visual Studio 2010, and it worked great after some minor tweaking of the regex.
The ? suffix for non-greedy search wasn’t accepted, and I had to use curly braces to store the string content so I ended up using this:


Wonderful post,

I think we also need to replace expression like T(‘A’) by juce_wchar(‘A’)
Am I right ?

To do this on Visual Studio we just need to find regular expression

and replace with

NB: do not forget to check “Match Whole word”.

[quote]I think we also need to replace expression like T(‘A’) by juce_wchar(‘A’)
Am I right ?[/quote]

I think that just ‘A’ should work correctly in almost any context, and in many circumstances will be than a wide-char.