Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string between two strings

Tags:

python

django

<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>

How would I get the string between the first two paragraph tags? And then, how would I get the string between the 2nd paragraph tags?

like image 502
Zorgan Avatar asked May 30 '17 09:05

Zorgan


People also ask

How do I extract a string between two characters?

Extract part string between two different characters with formulas. To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key.

How do you get a string between two strings in Python?

To find a string between two strings in Python, use the re.search() method. The re.search() is a built-in Python method that searches a string for a match and returns the Match object if it finds a match. If it finds more than one match, it only returns the first occurrence of the match.

How do I extract a string between two characters in SQL?

Use the SUBSTRING() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.


Video Answer


2 Answers

Regular expressions

import re
matches = re.findall(r'<p>.+?</p>',string)

The following is your text run in console.

>>>import re
>>>string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
>>>re.findall('<p>.+?</p>',string)
["<p>I'd like to find the string between the two paragraph tags.</p>", '<p>And also this string</p>']
like image 124
Isdj Avatar answered Oct 31 '22 20:10

Isdj


If you want the string between the p tags (excluding the p tags) then add parenthesis to .+? in the findall method

import re
    string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
    subStr = re.findall(r'<p>(.+?)</p>',string)
    print subStr

Result

["I'd like to find the string between the two paragraph tags.", 'And also this string']
like image 33
Rich Rajah Avatar answered Oct 31 '22 21:10

Rich Rajah