Regex append a string to a part of the match

geanyregex

I find this quite hard to do, the thing is as follows:

I have a string in the form:

GlobalParameters::$docId = DocList::$PARTNERS;

And I want to append the string _VIEW to the end of the symbol. The problem is that I want not only to match PARTNERS but any other symbol too, so I tried this regex:

Find:

GlobalParameters\:\:\$docId\ \=\ DocList\:\:\$(.*)\;

Replace with:

GlobalParameters\:\:\$docId\ \=\ DocList\:\:\$(.*)_VIEW\;

But I just got:

GlobalParameters::$docId = DocList::$(.*)_VIEW;

On all matches. How can I work around this?

Best Answer

The main thing wrong is that the matched field delimited by ( and ) needs to be identified by \1 in the replacement string:

GlobalParameters\:\:\$docId\ \=\ DocList\:\:\$\1_VIEW\;

It is also worth noting that, although your Geany implementation does not have this default, many programs which use regular expressions default to BRE (Basic Regular Expression) mode, which requires \( and \) to delimit the search subexpression, as in:

GlobalParameters\:\:\$docId\ \=\ DocList\:\:\$\(.*\)\;

Note: Geany as of v1.24 doesn't need the parentheses to be escaped, so you need just ( and ) to delimit matching fields.

Other, simpler search and replace strings occur to me, but without seeing the context of other strings in the file I am not sure which might be satisfactory. However, the following should be OK:-

Search:

\(GlobalParameters\:\:\$docId\ \=\ DocList\:\:\$.*\)\;

Replace:

\1_VIEW\;

Here the whole string apart from the trailing ; is matched and _VIEW is appended.

Related Question