3 Replies Latest reply on Oct 7, 2011 8:44 AM by sexcBoy

    Regular expression for removing only one return character

    wgb14 Level 1
      Hi all,

      Whats the regular expression for removing only one return character from a text string. I have something like that:

      1

      (two returns)

      2

      and i want to remove only one of the rrturn characters so i will get

      1
      2 (the one after the other

      I know /r but ot removes all returns from the text string.

      Many thanks for any help
        • 1. Re: Regular expression for removing only one return   character
          Level 7
          Try something like:
          (in my message window):
          s = "one" &RETURN&RETURN& "two"
          put s
          l = [s]
          put re_replace(l, "(?<=\r)\r", "g", EMPTY)
          put l[1]
          (output):
          -- "one

          two"
          -- 1
          -- "one
          two"

          I don't know how efficient the above pattern might be for searching, but
          you could also try:
          put re_replace(l, "\r\r", "g", RETURN)
          or:
          put re_replace(l, "\r{2}", "g", RETURN)
          however, these last 2 will only work in the situation where there are
          exactly 2 RETURNs you want to replace with a single RETURN - the first
          will replace any number of multiple RETURNs with a single RETURN

          This is perhaps more efficient than the first - it will again replace
          any multiple with a single; but it will needlessly replace a single
          RETURN character with a single RETURN character - but I'm not a RegEx
          expert:
          put re_replace(l, "\r+", "g", RETURN)
          • 2. Re: Regular expression for removing only one return character
            wgb14 Level 1
            Sean the first expression works nicely, the other two return syntax error. I think I can never thank you enough for all the help that you have provided me all this time!!!
            • 3. Re: Regular expression for removing only one return character
              sexcBoy Level 1

              The regular expression below will remove 1 character starting from the left.

              string = string.replace(/.$/, "");

               

              Using a function to return data is bad, make your code more strict by using a variable instead.

              some function in a programming language() {

                   x = 1 + 2;          // function with no return data.

              }

              log(x);

              ===============================================

              some function in a programming language() {

                   return 1 + 2;    // making function returning data is bad.

              }

              log(some function in a programming language());