Reflex to Delete all text except between two lines

雨燕双飞 提交于 2021-02-05 08:21:10

问题


I am trying to extract the content between two lines of string. Here is how the text looks

   Hhhshhajsjsjsj
    Hshhejjsjsmk
    Hahjajajajajja
     Message-ID: b123467
       abc
       def
       Kjhshjsjs
     Received:
  Hdjjddjdjdjdjd

I need to keep the text between ‘Message-ID’ and ‘Received:’

I tried ‘@“(?:\G(?!\A)[X-Message-ID:)\r?\n(.*)(?>\r?\nReceived:(?=S\r?$))?’

I have got an error ‘can’t find the text’


回答1:


Your problem would be straightforward if you only had a single occurrence of this text, and you only needed to extract the text inside the markers once. In this case, something like the following would work:

Message-ID:(.*?)Received:

But if you expect to have multiple matches in your file, then you need to do more work. Here is one pattern which should work:

(?:(?!Message-ID:).)*Message-ID:((?:(?!Received:)(?!Message-ID:).)*)Received:

Do a find on this pattern, and then replace with $1. Note that dot must be in newline mode, meaning that .* matches across newlines (this mode is enabled in the demo).

Demo




回答2:


You may use

(?s)Message-ID:(.*?)Received:|.

Replace with (?1$1\n).

Details

  • (?s) - turn on . matches newline option
  • Message-ID: - a literal substring
  • (.*?) - Group 1: any 0+ chars as few as possible
  • Received: - a literal substring
  • | - or
  • . - any 1 char.

The (?1$1\n) replacement means that if Group 1 matches, replace with Group 1 and append a newline after it, else, just remove the match.




回答3:


What does keep mean relative to the entire sample ? – sln yesterday

Sometimes it pays to answer questions.

Find (?s)\G(?:.*?Message-ID:(.*?)Received:|.*)
Replace $1

https://regex101.com/r/E3c3Ot/1



来源:https://stackoverflow.com/questions/50005618/reflex-to-delete-all-text-except-between-two-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!