Home > Software design >  How to print a substring between two patterns if an offset is provided as an input
How to print a substring between two patterns if an offset is provided as an input

Time:11-10

How can we print a substring that occurs between two patterns? We are also provided an offset of a character in the string as an input to choose which substring needs to be printed.

E.g.

string = '<p >A quick brown fox</p><p >Jumps over</p>'

The substring that needs to be printed is between the patterns "> and </p>.

If the offset provided is 20 then A quick brown fox should be printed. If the offset provided is 55 then Jumps over should be printed, this offset should be received as an input.

CodePudding user response:

The offset seems to be taken as a starting point for searching the nearest "> to the left and </p> to the right.

<p >A quick brown fox</p><p >Jumps over</p>
                    ^                                  ^
                 offset 20                          offset 55

In other words, find the rightmost "> in the substring from the beginning to the offset, and the leftmost </p> in the substring from the offset to the end.

For this, there are the find and rfind string methods.

>>> string.rfind('">', 0, 20)
13
>>> string.rfind('">', 0, 55)
49
>>> string.find('</p>', 20)
32
>>> string.find('</p>', 55)
61

Now you can use these indices to slice the string. You have to account for the fact that rfind returns the index before ">, but you need to slice after it, so you have to add its length (2) to the start index of the slice.

>>> start = string.rfind('">', 0, 20)   2
>>> end = string.find('</p>', 0, 20)
>>> string[start:end]
'A quick brown fox'
  • Related