1

I have the two following sentences:

1: KEYWORD this is a random sentence
2: this is another random sentence

I would like to match all after KEYWORD if KEYWORD exists, and if it doesn't, I want to match all.
So the output would be:

1: this is a random sentence
2: this is another random sentence

I tried a few things that did not lead me anywhere close:

(?<=KEYWORD)[\s\S]+
This would only match everything after the keyword, but will fail to capture anything if the keyword is not present...


PS: I use java
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Kylke
  • 47
  • 4

2 Answers2

3

See regex in use here

(?<=KEYWORD).*|^(?:(?!KEYWORD).)*$
  • Option 1
    • (?<=KEYWORD) Positive lookbehind ensuring what precedes is KEYWORD
    • .* Match any character (except \n) any number of times
  • Option 2
    • ^ Assert position at the start of the line
    • (?:(?!KEYWORD).)* Tempered greedy token matching any character except where KEYWORD matches
    • $ Assert position at the end of the line

Result:

this is a random sentence
this is another random sentence
ctwheels
  • 21,901
  • 9
  • 42
  • 77
3

As an alternative to ctwheel's nice working solution you may be able to use this regex that may perform faster because of no tempered greedy token that asserts a lookahead before every character.

(?<=KEYWORD |^(?!.*KEYWORD)).*

RegEx Demo

Details:

  • (?<=KEYWORD: If we have "KEYWORD " at previous position
  • |: OR
  • ^(?!.*KEYWORD): We don't have KEYWORD anywhere in the line
  • .*: Match 0 or more of any characters
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thanks :) That makes a lot of sense! No idea, why I haven't thought of this when I looked for a solution... – Kylke Feb 28 '18 at 16:39