Home > Net >  regex in google sheets, match previous period or semi-colon
regex in google sheets, match previous period or semi-colon

Time:01-06

I want the return to be The quick brown fox jumps over the lazy dog in both cases.

I'm using the formula =trim(regexextract(H4,"[\.;](.*?)\?\?"))

ex 1: blah blah. The quick brown fox jumps over the lazy dog?? blah blah I get the right answer.

but ex 2: blah balh. The quick brown fox jumps over the lazy dog. blah blah ; The quick brown fox jumps over the lazy dog??

I get The quick brown fox jumps over the lazy dog. blah blah ; The quick brown fox jumps over the lazy dog

not sure how to make it to lookback from the question mark to the first period or semi-colon since RE2 doesn't support lookback.

CodePudding user response:

To stop at the first non-alphanumeric, non-whitespace character, use [\w\s] , like this:

=trim(regexextract(H4, "[\.;]([\w\s] )"))

CodePudding user response:

You might use:

[.;]\s (.*?)[^\w\s]

Explanation

  • [.;] Match either . or ;
  • \s Match 1 whitespace chars
  • (.*?) Capture group 1, match any char as few as possible
  • [^\w\s] Match a single char other than a word char or whitespace char

See the capture group matches at the regex101 demo.

Example code:

=trim(regexextract(H4,"[.;]\s (.*?)[^\w\s]"))
  • Related