Home > Software engineering >  Regex for Two Empty Spaces at End of Sentence Containing Specified Word
Regex for Two Empty Spaces at End of Sentence Containing Specified Word

Time:08-19

Is there anyway to use regex to find consecutive whitespace characters at the end of a sentence containing a specified word? In the example below, I would want to capture just the two white spaces following the last "WORD" in which the sentence starts with "TOTAL." The number of words following the "TOTAL" would vary, but there wouldn't be an additional whitespaces between the words, just at the end.

TOTAL WORD WORD WORD WORD

I tried to use the following pattern (python flavor) in regex101:

(?:(?:TOTAL)(?:. ?(?=\s{2})))\s{2}

but couldn't find a way to exclude capturing the words prior to the whitespace.

CodePudding user response:

You could use something like this:

import re

string = 'TOTAL WORD WORD WORD WORD  '

match = re.findall('^TOTAL.*\w(   )$', string)
print(match)

Output:

['  ']

Explanation:

  • ^TOTAL start at the beginning of the sentence with "TOTAL"
  • .*\w match anything up to the last letter in the sentence
  • ( )$ capture any number of spaces greater than 2 at the end of the sentence
  • Related