Search code examples
regexgoogle-sheetssubstring

RegexReplace in GoogleSheets To Find Multiple Strings


I have a string such as the following

bells<1@gmail.com>,bars<2@gmail.com>, ballots<3@gmail.com> 

I would like to extract the e-mail addresses out of this string comma separated

Formula using is the following

=REGEXREPLACE(A7,"\<(.*?)\>","")

However, the results I get are the following and opposite of what I was expecting

bells,bars, ballots

This formula =REGEXEXTRACT(A7,"\<(.*?)\>") results in 1@gmail.com just fine, but I want to get all three 3 instances.

Any help and explanation as to why the regex "<(.*?)>" isn't working.


Solution

  • You can use

    =REGEXREPLACE(A1, "[^<]*<([^<>]*)>(,?)", "$1$2")
    

    See the regex demo. Details:

    • [^<]* - zero or more chars other than <
    • < - a < char
    • ([^<>]*) - Group 1: any zero or more chars other than < and > chars
    • > - a > char
    • (,?) - Group 2: an optional , char.

    $1 and $2 refer to the values captured with Group 1 and 2 respectively.