Search code examples
regexgoogle-sheets-formulansregularexpression

Need to extract String in between strings using REGEXEXTRACT() in Google Sheet


I have a string like this patern: "abcd abcd | abcde | Degree SP | xyz abcd | abcd ABC"

I need to extract "Degree SP" using regular expressions. How can I do that? The condition here are:

  • string end with "SP "
  • string start after last "|".

I'm trying the Google Sheet formula REGEXEXTRACT(<input string>, "[\|\s].+SR[\s\|]") It returns " | abcde | Degree SP ". How can I restrict to extract from the last "|"?


Solution

  • If the string Degree SP should be between pipes and a space:

    \|\s([^\s|][^|]*SP)\s\|
    
    • \|\s Match | and a whitespace char
    • ( Capture group 1
      • [^\s|] Match a single char other than a space or |
      • [^|]*SP Match optional chars other than | and match SP
    • ) Close group 1
    • \s\| Match a whitespace char and |

    Regex demo

    enter image description here

    If only the pipe after Degree SP is mandatory:

    ([^\s|][^|]*SP)\s*\|
    

    Regex demo

    enter image description here