Regular expression to extract whole sentences with matching word

前端 未结 4 928
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 06:20

I would like to extract sentences with the word \"flung\" in the whole text.

For example, in the following text, I\'d like to extract the sentence \"It was exactly

相关标签:
4条回答
  • 2020-12-03 06:30
    "[A-Z]\\s?\\w*\\s?(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*(?:your target\\s)(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*(([^(\\.\\s)|(\\?\\s)|(!\\s)])|\\s)*[\\.|\\?|!]"
    

    A sentence starts with any capital letter, in the middle it may contain decimal or abbreviation.

    0 讨论(0)
  • 2020-12-03 06:30
    (?<=^|\s)[A-Z][^!?.]*( word\s*)[^!?.]*(?=\.|\!|\?)
    

    Before first capital letter there is a line start or a white space, then it may consist any characters without set of [!?.](*)-or may not , then contains your target word with or without white spaces after it (if it is in the end of the sentence), then may consist again any characters without set of [!?.](*)-or not, and finally ends with dot or ! or ?.

    0 讨论(0)
  • 2020-12-03 06:32

    Here you go,

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

    DEMO

    OR

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

    DEMO

    0 讨论(0)
  • 2020-12-03 06:37

    Simply anything between dots:

    without a dote

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

    with a dote

    [A-Za-z," ]+word[A-Za-z," ]+\.
    
    0 讨论(0)
提交回复
热议问题