Using RegEx, how can we replace n blank/white spaces with n-1 blanks where n > 0.

已锁定 Using RegEx, how can we replace n blank/white spaces with n-1 blanks where n > 0.

  • Sunday, May 06, 2012 1:29 AM
     
     

    Example:
    Input: w1 w2   w3  w4
    Desired output: w1w2  w3 w4

    The input string has one, three and two blank spaces, respectively. And, the output string has zero, two and one blank spaces, respectively.

    Please help. Thanks,

    Nam

All Replies

  • Sunday, May 06, 2012 2:33 AM
     
     Answered

    Regex:  \s(\s*)
    Replace:  ${1}

    try:

    resultString = Regex.Replace(subjectString, @"\s(\s*)", "${1}");


    Ron

    • Marked As Answer by namwam Sunday, May 06, 2012 8:03 PM
    •  
  • Sunday, May 06, 2012 8:03 PM
     
     

    Thanks to Ron - his following suggestion works perfectly:

    resultString = Regex.Replace(subjectString, @"\s(\s*)", "${1}");

    Nam

  • Sunday, May 06, 2012 9:07 PM
     
     
    On Sun, 6 May 2012 20:03:31 +0000, namwam wrote:
     
    >
    >
    >Thanks to Ron - his following suggestion works perfectly:
    >
    >resultString = Regex.Replace(subjectString, @"\s(\s*)", "${1}");
    >
    >Nam
     
    Glad to help.  Thanks for the feedback.
     

    Ron