Search code examples
regexgoogle-sheetsgoogle-sheets-formularegex-groupre2

How do I match numbers after a particular string without a positive lookbehind?


Target file 39: Assignment4/Assignment4-sa471 - Sarah Iwobi.pde

I am attempting to match the numbers 471 after sa in the string above. This is possible using a positive lookahead in Python: (?<=sa)[0-9]+. Unfortunately, this is not possible in Google Sheets.

I attempted to use the regex expression [sa][0-9]+ but it includes the letter a in addition to the numbers: a471.


Solution

  • You may use

    =REGEXEXTRACT(A1, "sa(\d+)")
    

    Here, sa will be matched, and one or more digits after the substring will be matched and captured into Group 1.

    The REGEXEXTRACT function will return the captured value if a capturing group is defined in the regex pattern.

    See the RE2 regex demo.