Multiple regex matches in Google Sheets formula

后端 未结 5 2024
情歌与酒
情歌与酒 2020-12-01 17:08

I\'m trying to get the list of all digits preceding a hyphen in a given string (let\'s say in cell A1), using a Google Sheets regex formula :

=R         


        
5条回答
  •  天命终不由人
    2020-12-01 17:39

    This seems to work and I have tried to verify it.

    The logic is

    (1) Replace letter followed by hyphen with nothing

    (2) Replace any digit not followed by a hyphen with nothing

    (3) Replace everything which is not a digit or hyphen with nothing

    =regexreplace(A1,"[a-zA-Z]-|[0-9][^-]|[a-zA-Z;/é]","")
    

    Result

    1-2-2-2-2-2-2-2-2-2-3-3-
    

    Analysis

    I had to step through these procedurally to convince myself that this was correct. According to this reference when there are alternatives separated by the pipe symbol, regex should match them in order left-to-right. The above formula doesn't work properly unless rule 1 comes first (otherwise it reduces all characters except a digit or hyphen to null before rule (1) can come into play and you get an extra hyphen from "Patho-jour").

    Here are some examples of how I think it must deal with the text

提交回复
热议问题