Home > Enterprise >  I am trying to extract a substring from a given string using Regex on Google Sheets for the first ti
I am trying to extract a substring from a given string using Regex on Google Sheets for the first ti

Time:11-22

I have started learning Regex recently and I was given a string as follows:

T10BestFruits_Mango_7-Slices_Thick_Daily_noshare_BD5567#Aware#SeasonalFruits#NA#NA|FreshMangoes-6x5-NPM-5300000-Nov22

I want to extract 6x5 using REGEXEXTRACT function but I cannot find the right Regex pattern that will do it perfectly. Is there any regex pattern that will give me "6x5" directly without any delimiters or words attached?

I first tried using ([|.*?]\w*[-.*?]\w*[-.*?]\w*[-.*?]\w*[-.*?]\w*) to extract the substring but the result came out as |FreshMangoes-6x5-NPM-5300000-Nov22.

Then I tried [|](.*)[-] which gave me FreshMangoes-6x5-NPM-5300000 and -[\w*] - which gave me -6x5-.

What I needed was 6x5 exactly. There should be no delimiters in this substring.

CodePudding user response:

You can use

=REGEXEXTRACT(col, ".*\|.*?-(\w )")

See the regex demo. Details:

  • .* - any zero or more chars other than line break chars as many as possible
  • \| - a | char
  • .*? - any zero or more chars other than line break chars as few as possible
  • - - a hyphen
  • (\w ) - Group 1: one or more alphanumeric/underscore chars.

CodePudding user response:

use:

=REGEXEXTRACT(A1; "-(\d x\d )-")
  • Related