Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 51136

Summary: [find/replace] Regular expression replace does not handle groups
Product: [Eclipse Project] Platform Reporter: Ken Collins <ken>
Component: TextAssignee: Platform-Text-Inbox <platform-text-inbox>
Status: RESOLVED WORKSFORME QA Contact:
Severity: enhancement    
Priority: P3    
Version: 3.0   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Ken Collins CLA 2004-02-03 16:10:18 EST
If I find a group like "(\w+)" and try to use the group in a replacement string
like "found: \1" the group data is not inserted, and the group number is echoed
instead.
Comment 1 Dani Megert CLA 2004-02-04 03:04:16 EST
It supports groups. You can use \aGroupNumber in the find string and
$aGroupNumber in the replace string. It should support \aGroupNumber in the
replace string as well. I have to check why the regex package which we are using
isn't doing so. According to its doc (see F1) it should.
Comment 2 Ken Collins CLA 2004-02-04 12:53:13 EST
I got the $aGroupNumber syntax you described to work. Where did the
$aGroupNumber syntax come from? I'm only familiar with \aGroupNumber which is
standard for ex, sed, ed, and vim.

I couldn't get F1 to bring up help, and before I submitted this bug I did a Help
Contents search for regex and came up with nothing. I found docs on the "Finding
and Replacing" task, but it didn't mention regexes or group matching. Are the
docs not in 30m6, or are they not indexed?

Should I submit a new feature request to support the \aGroupNumber syntax or
just change the summary and status of this one?
Comment 3 Dani Megert CLA 2004-02-05 03:30:01 EST
>I got the $aGroupNumber syntax you described to work. Where did the
>$aGroupNumber syntax come from?
From java.util.regex. package.

>I couldn't get F1 to bring up help,
When in the Find field pressing F1 opens help. Does F1 work in other dialogs? If
not, first check your settings (Window > Preferences > Help) and if this does
not help file a bug against Platform Help.
Comment 4 Ken Collins CLA 2004-02-09 13:26:35 EST
>>I got the $aGroupNumber syntax you described to work. Where did the
>>$aGroupNumber syntax come from?
>From java.util.regex. package.

Is this documented in java.util.regex.Pattern? I didn't see anything that
mentioned '$' except for end of line matching. The groups are numbered, but I
don't know where '$' is associated with the group for substitutions.

I'll check 3.0m7 and see if \aGroupNumber in the replace strings is working again.

>When in the Find field pressing F1 opens help. Does F1 work in other dialogs? If
>not, first check your settings (Window > Preferences > Help) and if this does
>not help file a bug against Platform Help.

F1 isn't doing anything in either the find or replace fields. It's not working
elsewhere, either. I'll check in 3.0m7 when it comes out.
Comment 5 Dani Megert CLA 2004-02-09 13:36:24 EST
>I'll check 3.0m7 and see if \aGroupNumber in the replace strings is working >again.
It won't. We use the regex package upon which we do not have an influence. The
fact is that it works as I described. No idea why it is not documented along
with the regex package.

Note: F1 not working will not work with M7 if it doesn't work now.
Comment 6 Ken Collins CLA 2004-02-09 14:32:52 EST
> It won't. We use the regex package upon which we do not have an influence. The
> fact is that it works as I described. No idea why it is not documented along
> with the regex package.

So the replace is using something like matcher.replaceAll(), and this is why the
familiar "\aGroupNumber" syntax isn't working? Couldn't Eclipse's replace simply
be rewritten so that it substitutes \aGroupNumber for $aGroupNumber in the
replace string before applying it? Something like:

matcher.replaceAll(replaceString.replaceAll("\\\\(\\d)+", "\\$$1"));

If that's the case, can I reopen this as a feature request?

I think it would be better to use a widely adopted convention rather than one
unique to Java regex that's not documented.
Comment 7 Dani Megert CLA 2004-02-10 03:03:32 EST
Yes you can enter such a feature request. But first we have to verifiy what the
plan for the regex package is i.e. maybe it gets fixed there.
Comment 8 Ken Collins CLA 2004-02-10 15:24:06 EST
I opened bug 51530 as a feature request.