Multiple regex matches in Google Sheets formula

后端 未结 5 2029
情歌与酒
情歌与酒 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:36

    Edit

    I came up with more general solution:

    =regexreplace(A1,"(.)?(\d-)|(.)","$2")

    It replaces any text except the second group match (\d-) with just the second group $2.

    "(.)?(\d-)|(.)"
      1    2    3  
      Groups are in ()
      ---------------------------------------
     "$2" -- means return the group number 2
    

    Learn regular expressions: https://regexone.com


    Try this formula:

    =regexreplace(regexreplace(A1,"[^\-0-9]",""),"(\d-)|(.)","$1")

    It will handle string like this:

    "A1-Nutrition;A2-ActPhysiq;A2-BioM---eta;A2-PH3-Généti***566*9q"

    with output:

    1-2-2-2-3-

提交回复
热议问题