1

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?

2 Answers 2

0

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.

8
  • But why do the parentheses have to be escaped? They're not part of the string that I need to match, but anyway I'm going to try this solution as soon as I get to my computer. Thank you.
    – arielnmz
    Dec 9, 2014 at 22:25
  • The default for many programs which use regular expressions is BRE (Basic Regular Expression) mode, which requires escaping. See Wikipedia. When I tested your strings with sed I needed the back-slashes.
    – AFH
    Dec 9, 2014 at 22:40
  • I didn't knew that, but maybe Geany doesn't use that mode by default.
    – arielnmz
    Dec 9, 2014 at 22:42
  • I don't know Geany, so maybe you don't need the back-slashes, but you certainly need \1 in the replacement stream to expand the marked subexpression. Try using this first, then add the back-slashes to the brackets if you need to.
    – AFH
    Dec 9, 2014 at 23:29
  • Just tried it and works like a charm! Thank you! I really didn't know about grep's system to match fields, neat. Thank you again.
    – arielnmz
    Dec 10, 2014 at 14:58
0

To extend AFH's answer, I made a more complex example:

To-be-modified string:

abc: 123

Search:

(abc)\: (123)

Replace (1def will be appended to the first match expression and 456 to the second):

\1def \2456

Result:

abcdef 123456

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .