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 Quenot
  • 143,367
  • 32
  • 199
  • 195
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
  • 19,377
  • 6
  • 29
  • 60
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
  • 664,788
  • 59
  • 469
  • 547
  • 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