0

I am trying to remove full stops from a String. I have tried the below

titleAndBodyContainer = titleAndBodyContainer.replaceAll("\\.", " "); 

Unfortunately, this removed all other 'dots' as well. My paragraph includes dates like Jan.13, 2014 , words like U.S and numbers like 2.2. How can I remove only the full stops?

Sunil Singh Bora
  • 771
  • 9
  • 24
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 3
    So how do you differentiate a dull-stop from normal periods? – Rohit Jain Jan 31 '14 at 10:30
  • If new line after full stop has a space in between you can take advantage of that. – Aniket Thakur Jan 31 '14 at 10:30
  • How about `titleAndBodyContainer.replaceAll("\\.(\\s)", " $1");`? This will replace fullstops followed by a whitespace character with a space followed by the same whitespace character. – Zoltán Jan 31 '14 at 10:35
  • @Zoltán: what is that $1 ? Does that mean the fullstop will be replaces by a dollar mark? – PeakGen Jan 31 '14 at 10:38
  • No, it's a capture group. $1 represents the contents of the parentheses in the regex. It's like a regex variable. $1 represents the first parentheses, $2 the second and so on. Why not just try it? – Zoltán Jan 31 '14 at 10:41
  • Check out these: [No 1](http://stackoverflow.com/questions/21431011/net-regex-for-full-stop-at-the-end-of-a-sentence/21433495#21433495) and [No 2](http://stackoverflow.com/questions/21430447/how-to-split-paragraphs-into-sentences/21430792#comment32335355_21430792) – SamWhan Jan 31 '14 at 10:42
  • @GloryOfSuccess - Just noticed you asked one of them. Hmmm... Anyway, I'll repeat my comment: `This is not a trivial task, phew... E.g. the sentence "In Sweden a common name is Jan. 31, at least, people have this name.", would be virtually impossible to parse, without some kind of context aware parser. (Edit: "Jan" is a common christian name in Sweden ;))`. And since you've removed the space after Jan. you've got a working answer in [this one](http://stackoverflow.com/questions/21430447/how-to-split-paragraphs-into-sentences/21430792#comment32335355_21430792) – SamWhan Jan 31 '14 at 10:45
  • @ClasG: I am sure after the name there is no full stop. This situation is not applicable here. – PeakGen Jan 31 '14 at 10:49
  • This will be hard. You need to guarantee that your sentences are properly structured according to the rules of the english language. I'm sure you can work out why...an infinite number of mistakes could occur. Once you can guarantee that sentences are grammatically correct, you (I assume) can easily spot what is an end of a sentence. If you look into grammar, it is a world of pain for programmers :( Since there are many local variants, your solution imo will invariably fail at many points unless the sentences are grammatically structured in what you define as correct. My 2 senses worth :) – gwillie Jan 31 '14 at 11:55

3 Answers3

2

I'd do:

titleAndBodyContainer = titleAndBodyContainer.replaceAll("\\.(?=\\s|$)", " "); 
Toto
  • 89,455
  • 62
  • 89
  • 125
0

You can check if any space or newline coming after . .

You can use the regex : (\.(?=[\s\n\r]|$))

Code :

 String input = "Unfortunately, this removed all other 'dots' as well. My paragraph includes dates like Jan.13, 2014 , words like U.S and numbers like 2.2. How can I remove only the full stops?\n"+"Somthing is there.\n"+"Hello someone . What is this something ? ";

 String REGEX = "(\\.(?=[\\s\\n\\r]|$))";

 String x=input.replaceAll(REGEX, " ");

OUTPUT

Unfortunately, this removed all other 'dots' as well My paragraph includes dates like Jan.13, 2014 , words like U.S and numbers like 2.2 How can I remove only the full stops?
Somthing is there
Hello someone  What is this something ? 

DEMO

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
  • [But even then](http://regex101.com/r/oS8jC7)... And I'm sure I can come up with more exceptions. Conclusion, this task is not suitable for regex. – Jerry Jan 31 '14 at 11:17
0

If your strings (sentences) are grammatically correct i.e. a sentence following the fullstop starts with a capital letter I think you can create a custom method to check for such fullstop if after the fullstop there is a string having its first letter capitalized.

Omoro
  • 972
  • 11
  • 22