Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 51530 - [find/replace] Regular expression replace does not recognize \1 group syntax
Summary: [find/replace] Regular expression replace does not recognize \1 group syntax
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.4 M1   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-02-10 15:22 EST by Ken Collins CLA
Modified: 2007-07-15 10:30 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ken Collins CLA 2004-02-10 15:22:31 EST
With regular expressions enabled in the find/replace dialog, you can currently
echo matched groups using the syntax $groupNumber in the replace string. So for
example, with the find string "(\w)" you can echo the matched group with "$1" in
the replace string.

However, the group echo syntax used for ex, sed, ed, vim, and other GNU utilites
is \groupNumber. I would like to see \groupNumber as an accepted syntax for
echoing groups.

It appears that java.util.regex is used for the underlying replacement, and
adding this feature may be as simple as applying the following substitution to
the replace string before it is used:

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

This was opened as <a
href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=51136">bug 51136</a> originally.
Comment 1 Dani Megert CLA 2004-02-11 05:49:02 EST
We should find out whether this is a general regex problem or only a problem in
some VMs.

As a workaround we should offer \group syntax.
Comment 2 Markus Keller CLA 2007-06-14 15:05:57 EDT
Just changing "\0" to "$0" in replace strings would make a certain set of replacements impossible. The fix for bug 80667 contains a workaround for this problem:

/*
 * Feature in java.util.regex.Matcher#replaceFirst(String):
 * $00, $000, etc. are interpreted as $0 and
 * $01, $001, etc. are interpreted as $1, etc. .
 * If we support \0 as replacement pattern for capturing group 0,
 * it would not be possible any more to write a replacement pattern
 * that appends 0 to a capturing group (like $0\0).
 * The fix is to consider \00 and $00 as $0\0, and
 * \01 and $01 as $0\1, etc.
 */
Comment 3 Dani Megert CLA 2007-07-15 10:30:51 EDT
Thanks Markus.
Committed to HEAD.
Available in builds > N20070715-0010.