tags:

views:

199

answers:

3

In particular: I want to replace all occurances of a full stop \. with a full stop plus white space \.\s if the full stop is immediately followed by an upper case character [A-Z].

Any suggestions? thanks

+2  A: 

You can try the regex s/\.([A-Z])/. \1/g, like so:

myString.replaceAll("\\.([A-Z])", ". $1");

For more, see the Pattern class and String.replaceAll. Note that backslashes need to be escaped themselves, hence the double backslashes.

Stephan202
Hi Stephan - thanks for help - however not quite there: the second arg to replaceAll is a string literal, not a regex, so the back ref \\1 is not interpreted
Richard
@Richard: You're right. I just noticed this after testing it. Fixed it. (Initially I wrote the code by just looking at the documentation, which I apparently misinterpreted.)
Stephan202
ok! that works with $1. I would never have figured that out.... cheers
Richard
+3  A: 
yourstring.replaceAll("\\.([A-Z])",". $1")
jitter
thanks - that's it - couldn't figure out how to do the back ref.
Richard
A: 

why not use positive lookahed?

(\\\.)(?=[A-Z])

skwllsp