Home > Software engineering >  Regexextract text just before specific text Google Sheets
Regexextract text just before specific text Google Sheets

Time:11-18

I need to extract the text that comes before the text District

Example String:

9224 H63, Ban Suan, Chon Buri District, Chon Buri 20000, Thailand

Valid Output should be Chon Buri District.

I tried =REGEXEXTRACT(K661,",\s([^\District] ") but it did not work.

CodePudding user response:

You can use

=REGEXEXTRACT(K661, ",\s*([^,]*?District)")

See the regex demo.

Details:

  • , - a comma
  • \s* - zero or more whitespaces
  • ([^,]*?District) - Capturing group 1: any zero or more chars other than a comma as few as possible and then a District fixed string.
  • Related