-1

I have a string of the following form :

String s = "......????!!!!! I am doing!!!???good , you say!!! ...how are...??!! you doing!?????.....";

and I want the output of this string as follows :

"I am doing!!!???good you say how are you doing"

and I am not able to come up with a regular expression that can do this for me, what I am trying is a brute force way of scanning the string and appending to the text, but there are many cases to test this out, if someone can help me figure out a regex or a more efficient way to do this it would be great.

More specifically I want to be doing this : remove all the leading and trailing terminators but retain those that occur within a string as in "say????what"

AnkitSablok
  • 3,021
  • 7
  • 35
  • 52
  • Take a look at [this](http://stackoverflow.com/a/15567045/1558430). You will need to modify it to fit your needs, however. – Brian Sep 28 '13 at 01:48
  • 1
    I think it might be better to *ban* any user who use more than one punctuation mark at the end of a sentence :-) – Stephen C Sep 28 '13 at 01:55
  • I actually require this for a parsing routine, where I am just parsing some random text, but just can't seem to get the regex :( – AnkitSablok Sep 28 '13 at 02:01

1 Answers1

2

This will work for your example:

s = s.replaceAll("(?<=^| )[.?!]+|[.?!]+(?= |$)", "");

Tested this line and it produces your requested output.

It deletes all punctuation sequences next to a space or at either end.

Bohemian
  • 412,405
  • 93
  • 575
  • 722