Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

Java, the OLPC, and community responsibility

The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Replies: 5 - Pages: 1  
  Click to reply to this thread Reply

newlines in regex find & replace

At 9:02 PM on Feb 19, 2006, Rob Agar Javalobby Newcomers wrote:

In find and replace using regexes, how do you include newlines and other control characters into the replace text?

I assumed it would be the normal regex \n, but the backslash just gets stripped leaving a literal "n".
  Click to reply to this thread Reply
1. At 8:22 AM on Feb 20, 2006, Riyad Kalla Javalobby Editors wrote:

Re: newlines in regex find & replace

You need to escape your escape char, confusing right? Try \\n, if that doesn't work, keep adding slashes until it does (I'm not kidding, weird hu?)

There is some information about this in the Javadoc for the Pattern/Matcher classes in the JDK, about escaping escape sequences.
Best, Riyad [kallasoft | The "Break it Down" Blog]
  Click to reply to this thread Reply
2. At 3:24 PM on Feb 20, 2006, Dominik G. Blooming Javalobby Member wrote:

Re: newlines in regex find & replace

i've just played around with find/replace a bit and it seems replace only supports reading matching groups from the 'find' expression(see content assist with $i). If you uncheck 'regular expression' in find the regex option is disabled in replace.

Interesting sidenote:
following Riyads advice i tried adding more backslashes:
\t -> t (no surprise here)
\\t -> \t (not tabulator but text)
\\\t -> \t (same.. wheres that extra backslash?)
\\\\t -> \\t (duh, i got bored then)

eclipse version used:
Version: 3.1.0
Build id: I20050627-1435

i was really surprised by this, i am used to do find/replace stuff with textpad on windows, but this is a common task and eclipse should be able to do it.

also see bug52338
my view on eclipse eclipse irc channels: #eclipse #eclipse-de
  Click to reply to this thread Reply
3. At 8:24 PM on Feb 20, 2006, Rob Agar Javalobby Newcomers wrote:

Re: newlines in regex find & replace

> also see bug52338

the most recent comment on the bug says this:

find: \n
replacewith: "+\n"

This will just delete all lines in the code.

Rather an alarming bug for something as apparently straightforward as search+replace! Definitely worth a bugzilla vote.
  Click to reply to this thread Reply
4. At 6:28 AM on Oct 26, 2007, Juan Javalobby Newcomers wrote:

Re: newlines in regex find & replace

I found a little work-around. To get a new-line character, type the following in Eclipse:
--snip--
c
c
--snip--
(if the above doesn't format correctly for some reason, it is just a character on one line, and a character on the line below it)

Copy the two characters into the "Replace with" box and you'll now see something like:
c{weird character}c
Now, just replace the two c's with your own regexp stuff. The weird character will work as a newline.
(PS my editor's encoding is set to UTF-8 - don't know if this is important to know for this workaround.)
  Click to reply to this thread Reply
5. At 2:25 PM on Oct 26, 2007, Suresh Krishna Occasional Javalobby Visitor wrote:

Re: newlines in regex find & replace

Yes Dominik, i too found the same effect. Looks like the replace does not really support the control chars. In fact there are many chars supported in the find but not in replace. Its ironic somehow.
~Suresh Krishna SureshKrishna Sciology

thread.rss_message