Regex newbie: How to isolate 'num-num-num' in a string -
i'm sure super simple question many of you, i've started learning regex , @ moment can't life of me isolate i'm after following:
june 2015 - won / void / lost = 3-0-1
i need solution isolate 'num-num-num' part @ end of string work positive integers.
thanks help
edit
so line of code scrapy spider i'm writing produces line above:
tips_str = sel.xpath('//*[@class="recent-picks"]//div[@class="title3"]/text()').extract()[0]
i've tried isolate part i'm after with:
tips_str = sel.xpath('//*[@class="recent-picks"]//div[@class="title3"]/text()').re(r'\d+-\d+-\d+$').extract()[0]
no luck though :(
the regex capture is:
\d+-\d+-\d+$
it works follows:
\d+-
means: capture 1 or more digits (the numbers[0-9]
), , "-".$
means: should @ end of line.
translating full regex pattern:
capture 1 or more digits, hyphen, 1 or more digits, hyphen, 1 or more digits, , should @ end of string.
edit: addressing edits , comments:
i'm not sure mean "isolate". i'll assume mean want tips_str
equal "3-0-1".
i believe easiest way first use xpath extract string entire line without doing regex. then, when we're dealing string (instead of xpath stuff), should nice , easy use regex , pattern out.
as far understand, sel.xpath('//*[@class="recent-picks"]//div[@class="title3"]/text()').extract()[0]
(without .re()
) providing string: "june 2015 - won / void / lost = 3-0-1".
so then:
full_str = sel.xpath('//*[@class="recent-picks"]//div[@class="title3"]/text()').extract()[0]
now we've got full string, can use standard string regex pluck part want out:
tips_str = false search = re.search(r'\d+-\d+-\d+$', full_str) if(search): tips_str = search.group(0)
now tips_str
equal "3-0-1". if pattern wasn't matched @ all, it'd instead equal false
.
if of assumptions wrong let me know what's happening (like if .extract()[0]
isn't giving string, giving back?) , i'll try adjust response.
Comments
Post a Comment