I have the following regex https://regex101.com/r/6orbpa/1
^ho..ital\s (?:(?:\b[a-zA-Z_'] \b)[ ]*){1,5}$
it extracts any senetence that starts with the word hospital OR hopital and only includes the sentence if it has up to 5 words to the right.
How do I modify the the regex to make it work from when the words ENDS in hospital OR hopital and includes up to 5 words to the left?
CodePudding user response:
You can use
^(?:[a-zA-Z_'] ){1,5}hos?pital$
See the regex demo. In case you are not using multiline texts, you may replace the literal space with \s in the pattern.
Details:
^- start of string(?:[a-zA-Z_'] ){1,5}- one to five occurrences of[a-zA-Z_']- one or more letters, underscores or'chars- one or more spaces
hos?pital-hospitalorhopital$- end of string.
