Community
Participate
Working Groups
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.
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); }
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 ***