Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 376438 - [find/replace] unable to replace space between word boundaries with another character
Summary: [find/replace] unable to replace space between word boundaries with another c...
Status: CLOSED DUPLICATE of bug 109481
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.8   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-10 15:53 EDT by Yiming Sun CLA
Modified: 2012-05-11 14:13 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yiming Sun CLA 2012-04-10 15:53:29 EDT
Build Identifier: 20120216-1857

I was trying to use the IDE find/replace feature (ctrl+F, or Edit->Find/Replace) to replace single spaces between word boundaries with another character using regex.  It was able to find these occurrences in the file, but would not replace them.

Reproducible: Always

Steps to Reproduce:
1. create a text file in eclipse, and give it some texts containing words separated by a white space (ASCII 32), e.g.

The spaces in this sentence should be replaced with underscores


2. press ctrl+F to bring up the find/replace dialog box

3. in the "Find:" text box, enter the following regular expression:
    \b\s\b
 which is to find single whitespace between words

4. in the "Replace with:" text box, enter some character, e.g. the underscore _

5. Make sure "Regular expressions" is checked

6. click "Find/replace".  The cursor in the IDE moves and highlights the next match, but it actually did not replace the previous match.

7. Click "Replace All", and the dialog box would tell you "x matches replaced", but it actually did not replace any.
Comment 1 Dani Megert CLA 2012-04-11 08:33:29 EDT
This is a bug in the JRE:

	public static void main(String[] args) {
		Matcher matcher;
		Pattern pattern= Pattern.compile("\\b\\s\\b"); // does not work
//		Pattern pattern= Pattern.compile("\\s"); // works
		matcher= pattern.matcher("The spaces in this sentence should be replaced with underscores");
		matcher.find();
		String prevMatch= matcher.group();
		Matcher replaceTextMatcher= pattern.matcher(prevMatch);
		String replaced= replaceTextMatcher.replaceFirst("_");
		System.out.println(replaced);
	}
Comment 2 Markus Keller CLA 2012-05-11 14:13:28 EDT
Nope, the JRE is fine:

        String input= "The spaces in this sentence should be " +
        		"replaced with underscores";
        System.out.println(input.replaceAll("\\b\\s\\b", "_"));

The problem here is that the FindReplaceDocumentAdapter uses Matcher#replaceFirst(..) in a wrong way. It really has to use a matcher that sees the whole document.

*** This bug has been marked as a duplicate of bug 109481 ***