Ruby Question

delete the beginning and the end string with regex

I need a regular expression that can eliminate the beginning and end of a string. example i have:

<br><br><br>hi how are you<br><br><br>hi how are you<br>hi how are you<br>hi how are you<br><br><br><br>


I need to delete the beginning and the end and just keep a
<br>
at the beginning of the string and end

<br>hi how are you<br><br><br>hi how are you<br>hi how are you<br>hi how are you<br>

Answer
str = " <br><br><br>hi how ru<br><br><br>hi how ru<br>hi how ru<br><br><br><br>"

r = /
    \A         # match beginning of string
    (?:<br>)+  # match <br> one or more times
    (?=<br>)   # match <br> in a positive lookahead
    |          # or
    (?<=<br>)  # match <br> in a positive lookbehind
    (?:<br>)+  # match <br> one or more times
    \z         # match end of string
    /x         # free-spacing regex definition mode

str.strip.gsub(r,"")
  #=> "<br>hi how ru<br><br><br>hi how ru<br>hi how ru<br>"

Here is another way, one that does not use a regex.

(str.strip).sub(/(?:<br>)+/, "<br>").reverse.sub(/(?:>rb<)+/, ">rb<").reverse
  #=> "<br>hi how ru<br><br><br>hi how ru<br>hi how ru<br>"