Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to find all occurrences of a full stop "." followed by a capital letter in a string and have these replaced with a "; ". How do I do this in Mathematica?

e.g. string="this is a test.A new sentence here" -> output

"this is a test; A new sentence here"

Edit: and also one other pattern matching question - in a string I would like to have every other line occuring before a next line (\n) being deleted - how do I do that one?


string="this line should be deleted\nthis one not\nbut this one should be deleted too\nthis one not"

-> output

"this one not\nthis one not"
share|improve this question
up vote 6 down vote accepted

Some variety:

StringReplace[string, ("." ~~ char : LetterCharacter /;UpperCaseQ[char]) :> "; " <> char]

"this is a test; A new sentence here"

As for your second question, one way to go about it would be:

StringJoin[Riffle[StringSplit[string, "\n"][[2 ;; -1 ;; 2]], "\n"]]
"this one not
 this one not"
"this one not\nthis one not"
share|improve this answer
Thanks a lot, and also for the other answer! – Tom Wenseleers Nov 18 '13 at 13:12
Oh yes and would you also happen to know the answer to the other question I put in my edited post? – Tom Wenseleers Nov 18 '13 at 13:19
@TomWenseleers it will be better to ask separate question. – Kuba Nov 18 '13 at 13:31
Thanks so much - brilliant! – Tom Wenseleers Nov 18 '13 at 14:11
StringReplace["this is a test.A new sentence here", RegularExpression["\\.([A-Z])"] -> "; $1"]

The $1 is needed to carry the capital letter into the returned string or it will be lost.

share|improve this answer
Hey, $1 per replacement can get expensive quickly! – Yves Klett Nov 18 '13 at 12:26
Its a penalty designed to encourage correct punctuation in the first place. – Ymareth Nov 18 '13 at 12:59
Talk about cost functions, eh? – Yves Klett Nov 18 '13 at 13:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.