skip to Main Content

I’m trying to match all lines of text up until a certain specific sentence in a text file using the VSCode editor, but have failed so far after hours of ‘trying to figure regex out’ – unfortunately there’s on only machine logic to regex no human logic ; )

Something like:

(.?)<specific sentence>

The (.?) should represent ANYTHING – including whitespace and newlines.

I want
to remove
all this text
no matter what
the characters are
up until
THIS SENTENCE <-- I want to keep this sentence (or replace the match with it)

I’m trying to get rid of a long intro text in many documents, so therefore the need of a regex to do the job.

I simply cannot figure out how to also include whitespace and newlines. I’ve tried adding all combinations of [n]+, [r]+, [nr]+ (and a million more) I could think of – nothing works.

2

Answers


  1. Chosen as BEST ANSWER

    And, as always, only AFTER asking the question did I find the answer:

    ((.|n)*)
    <specific sentence>
    <new line character>
    

  2. You could try this regex, and replace it with nothing.

    ^[rsS]+?(?=^THIS SENTENCE)
    

    Normally [sS] would be sufficient to match any charater including line breaks, but in VSCode it isn’t. By adding r to the set should make it work as intended.

    See the test case

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search