Home > Enterprise >  How to search content between two strings, last match of start string first match of end string in p
How to search content between two strings, last match of start string first match of end string in p

Time:06-16

How to search content between two strings, last match of start string first match of end string?

Example:

string = 'Part 1. Part 1. Part 2. Part 3 then more text'

I want to extract the content between Part 1 (inner most match) and Part 3 expected output is Part 2.

re.search(r'Part 1\.(.*?)Part 3', s).group(1) gives ' Part 1. Part 2. '

CodePudding user response:

You used .*? (non-greedy match) expecting to get the smallest substring between Part 1. and Part 3. But it captures from the first Part 1. till to the first occurrence of Part 3. So, you should negate the Part 1. in the capturing group

Part 1\.((?:(?!Part 1\.).)*)Part 3

See the regex demo

Python Example

re.search(r"Part 1\.((?:(?!Part 1\.).)*)Part 3", string).group(1)  # ' Part 2. '

CodePudding user response:

I found the similar question

(?<=Part 1)(((?!Part 1).)*)(?=Part 3) would work well.

  • Related