0
"\r\n\r\n\r\n\r\nCovered. Coverage is limited to a one-time only 7095 for one outpatien" +
"t in vitro fertilization procedure while you are an 7095 member.  If you receive" +
" 7095s for in vitro fertilization services under an 7095 plan, you will not b" +
"e eligible for in vitro fertilization 7095s under any other 7095 plan. In vit" +
"ro fertilization services are not covered when a surrogate is used.  The in vitr" +
"o procedures must be performed at a medical facility that conforms to the Americ" +
"an College of Obstetricians and Gynecologists’ guidelines for in vitro fertiliza" +
"tion clinics or to the American Society for Reproductive Medicine’s minimal stan" +
"dards for programs of in vitro fertilization. \r\n\r\n\r\n\r\n\r\nIf you have a male partn" +
"er, you must meet all of the following criteria:\r\n•   You and your male partner " +
" have a five-year history of infertility or infertility is related to one or mor" +
"e of the following medical conditions: \r\n–         Endometriosis; \r\n–         Ex" +
"posure in utero to diethylstilbestrol (DES); \r\n–         Blockage or surgical re" +
"moval of one or both fallopian tubes; or \r\n–         Abnormal male factors contr" +
"ibuting to the infertility.\r\n•   You and your male partner have been unable to a" +
"ttain a successful pregnancy through other covered infertility treatments.\r\n\r\n\r\n" +
"\r\n\r\nIf you do not have a male partner, you must meet the following criteria:\r\n• " +
"  You are not known to be otherwise infertile, and\r\n•   You have failed to achie" +
"ve pregnancy following three cycles of physician directed, appropriately timed i" +
"ntrauterine insemination.\r\n\r\n\r\n\r\n\r\nPlease note: These services must have precert" +
"ification.  See Chapter 5: Precertification.\r\nPlease note: Exclusions or limits " +
"that may relate to this 7095 are described in Chapter 6: Services Not Covered" +
" in the section labeled Fertility and Infertility.\r\n\r\n\r\n\r\n"

I want to replace /r/n in above string with empty string with following restrictions. 1. Don't not replace /r/n if its exists only once (Ex: /r/n) 2. Replace if multiple /r/n (Ex: /r/n/r/n/r/n/r/n then replace with empty string.

Example String "\r\n\r\n\r\n\r\nCovered. Coverage is limited to a\r\n one-time only 7095 for one outpatient\r\n\r\n\r\n";

Expected Output: Covered. Coverage is limited to a\r\n one-time only 7095 for one outpatient

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    Use a regex like `"(the sequence you are looking for){2,}"` and replace it with nothing. – luk2302 Nov 05 '18 at 17:42
  • could you provide me a sample example? –  Nov 05 '18 at 17:54
  • Welcome to StackOverflow. As explained in the [tour], this site is a repository of useful questions and their answers, ***not a help forum***. Please take the [tour], visit the [help] and especially read [ask] and [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) to learn how to use this site effectively. – Jim Garrison Nov 05 '18 at 18:00
  • thank you.. solution worked for me –  Nov 05 '18 at 19:43

1 Answers1

0

I would write something like this:

my_str = my_str.replaceAll("(\r\n){2,}", "");

Hope that helps.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44