Search code examples
regexgoogle-sheetsspreadsheettransposetextjoin

How do I use regex in sheets to number (in cell) everything new line that starts with Letters?


My goal is to get something like this goal. For every new line after the 1st one if it doesn't start with a ">" I want it to be numbered (starting from nr. 1 on), as for the lines that start with ">" - to just return those without numbering them. I've tried to insert strings with the numbers See here, the problem is I don't know how many lines I might have that don't start with ">" and if these are intercalated with the other ones - so I can't really "hardcode" the string into the formula, as I did. Any solutions? thanks.


Solution

  • try:

    =ARRAYFORMULA(TEXTJOIN(CHAR(10), 1, IF(REGEXMATCH(""&
     SPLIT(A2, CHAR(10)), "^>.*"), 
     SPLIT(A2, CHAR(10)), TRANSPOSE(MMULT(TRANSPOSE(TRANSPOSE((SEQUENCE(1, COLUMNS(
     SPLIT(A2, CHAR(10))))<=SEQUENCE(COLUMNS(
     SPLIT(A2, CHAR(10)))))*NOT(REGEXMATCH(
     SPLIT(A2, CHAR(10)), "^>.+")))), TRANSPOSE(SIGN(NOT(REGEXMATCH(
     SPLIT(A2, CHAR(10)), "^>.+"))))))&". "&
     SPLIT(A2, CHAR(10)))))
    

    enter image description here