Regular expression to extract whole sentences with matching word

I would like to extract sentences with the word "flung" throughout the text.

For example, in the following text I would like to extract the sentence "It was exactly as if the hand grabbed them in the center and threw them aside." using a regular expression.

I tried using this one .*? flung (?<sub>.*?)\.

, but it starts searching from the beginning of the string.

How can I solve the problem?

How she did it, the most unusual thing happened. The linens came together, suddenly jumped into some kind of peak, and then jumped onto the lower rail. As if a hand grabbed them in the center and threw them aside. Right after,.........

+4


source to share


4 answers


Here you go,

[^.]* flung [^.]*\.

      

DEMO



OR

[^.?!]*(?<=[.?\s!])flung(?=[\s.?!])[^.?!]*[.?!]

      

DEMO

+13


source


Just nothing in between the dots:

without object

[A-Za-z," ]+word[A-Za-z," ]+

      



with label

[A-Za-z," ]+word[A-Za-z," ]+\.

      

+3


source


"[A-Z]\\s?\\w*\\s?(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*(?:your target\\s)(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*[\\.|\\?|!]"

      

The sentence starts with any capital letter, in the middle it can contain a decimal or an abbreviation.

0


source


(?<=^|\s)[A-Z][^!?.]*( word\s*)[^!?.]*(?=\.|\!|\?)

      

The beginning of the line or space begins before the first capital letter, then it can contain any unset characters [!?.](*)-

or cannot, then contains your target word with or without white spaces (if it is at the end of the sentence), then it can contain any unset characters again [!?.](*)-

or not, and finally ends with a dot or !

or ?

,

-1


source







All Articles