问题
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 optionMessage-ID:
- a literal substring(.*?)
- Group 1: any 0+ chars as few as possibleReceived:
- 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