请问这个正则表达式如何写

2015 年 5 月 28 日
 sicongliu
<dt><a name="313"></a>ADHE 313 (6) <b>Organization of Adult Basic Education Programs</b></dt>

想抓出ADHE313和Organization of Adult Basic Education Programs
4362 次点击
所在节点    Python
18 条回复
asj
2015 年 5 月 28 日
这难道不是应该用CSS/JQuery selector,或者XPath么?
phx13ye
2015 年 5 月 28 日
<\/a>(.*)<b>(.*?)<\/b>
sicongliu
2015 年 5 月 28 日
xpath比较简单 但是想学下正则的方法
shoumu
2015 年 5 月 28 日
看一下pyquery吧,支持jQuery的语法
professorz
2015 年 5 月 28 日
.+<\\/a>(.+)(6)<b>(.+)<\\/b>.+
java下的regex
sicongliu
2015 年 5 月 28 日
python的如何写
yiyiwa
2015 年 5 月 28 日
python测试了一下,不完善,有空的东西。

'\>([^\<]*)\<'
sicongliu
2015 年 5 月 28 日
m=re.search("</a>(.*?)\s(",text)
print (m.group(1))

m=re.search("<b>(.*?)</b>(",text)
print (m.group(1))
sicongliu
2015 年 5 月 28 日
如果要取ADHE 313呢?
如何判断第二个空格?当然用字符串的search切片功能很容易达到,只是想知道正则如何达到
sicongliu
2015 年 5 月 28 日
m=re.search("</a>(.*?)\s+\(",text)
print (m.group(1))

当然方法比较笨,如果第二个空格后不是“(”就没办法了
asj
2015 年 5 月 28 日
简单写了一个,还很不完善
(?:<dt.*?>)(?:.*?\/.*?>)([\w ]*)(?:.*?)(?:<\/dt>)

http://regexr.com/3b3bs
fy
2015 年 5 月 28 日
这个需求不用正则,会简单得多

page.xpath("//dt/text()") -> ADHE 313 (6)
page.xpath("//dt/b/text()") -> Organization of Adult Basic Education Programs
picasso250
2015 年 5 月 28 日
/a>([\w ()]+)<b>([\w ]+)</b>
最简单的解决了你现在的问题。
picasso250
2015 年 5 月 28 日
对不起,上一个是错误的,多提取了(6)

/a>(\w+ \d+).+?<b>([\w ]+)</b>
leozy2014
2015 年 5 月 28 日
print re.findall('</a>(.*?) \(6\) <b>(.*?)</b></dt>', s)
#[('ADHE 313', 'Organization of Adult Basic Education Programs')]
wmttom
2015 年 5 月 28 日
python正则 (?<=>)[\w, ,\(,\)]+?(?= \(|<)

re.findall("(?<=>)[\w, ,\(,\)]+?(?= \(|<)", '<dt><a name="313"></a>ADHE 313 (6) <b>Organization of Adult Basic Education Programs</b></dt>')

['ADHE 313', 'Organization of Adult Basic Education Programs']
sicongliu
2015 年 5 月 29 日
楼上两个貌似都不能用
sicongliu
2015 年 5 月 29 日
sorry这个可行

print re.findall('</a>(.*?) \(6\) <b>(.*?)</b></dt>', s)
#[('ADHE 313', 'Organization of Adult Basic Education Programs')]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/194398

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX