简体   繁体   中英

python regular expression, positive look behind

I am trying to correct this code and keep getting

sre_constants.error: look-behind requires fixed-width pattern

Please help me on get rid of this error... what I am trying to do is to get the numbers which is the variable w2 that comes right after word(s) which is the variable w .

import requests
import re
import bs4


def verse(book, chapter):
        html = requests.get("http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL={}&CN={}&CV=99"
                            .format(book, chapter)).text
        bs = bs4.BeautifulSoup(html, 'html5lib')
        ol = bs.findAll('ol')  
        section_cnt = int(ol[-1].attrs['start']) + len(ol[-1].findAll('li')) - 1
        w = re.search(r'(?<=height=12>\s<b>)(\d+\s)?[a-zA-Z]+\s[0-9]+', html).group()
        w2 = re.search(r'(?<=height=12>\s<b>(\d+\s)?[a-zA-Z])+\s[0-9]+', html).group()

        print(w, 'has', w2, 'chapters', section_cnt, 'verses')

if __name__ == '__main__':
    verse(1, 27)

You dont need lookbehind here.

Use

(?:height=12>\s<b>(?:\d+\s)?[a-zA-Z]+)(\s[0-9]+)

See demo.

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

Get group 1 instead.

w2 = re.search(r'(?:height=12>\s<b>(?:\d+\s)?[a-zA-Z]+)(\s[0-9]+)', html).group(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM