Redian新闻
>
简单python regular expression 问题求救
avatar
简单python regular expression 问题求救# Programming - 葵花宝典
a*i
1
简单python regular expression 问题求救
怎么写一个 regular expression 去search 象下面的expression,
Page 1 of 25
particularly, I want to read out the number 25 from above expression, I
have tried a few things, cannot make it works, thanks in advance.
avatar
s*d
2
import re
p = re.compile(r"Page\s+(\S+)\s+of\s+(\S+)")
m = p.search("Page 1 of 25")
print m.group(0), m.group(1), m.group(2)
avatar
a*i
3
Thanks.
My question is, I want to find out sentence like:
Page 1 of 25
in a large file, then from it, I need to read the number similar to "25", it
can be any number, for example,
Page 1 of 33
Will your solution works ?

【在 s*******d 的大作中提到】
: import re
: p = re.compile(r"Page\s+(\S+)\s+of\s+(\S+)")
: m = p.search("Page 1 of 25")
: print m.group(0), m.group(1), m.group(2)

avatar
r*t
4
Why should not his solution work?
自己不看文档,别人花时间给你做了,
你不感谢不说,还没试一下就在这儿质疑。

it

【在 a***i 的大作中提到】
: Thanks.
: My question is, I want to find out sentence like:
: Page 1 of 25
: in a large file, then from it, I need to read the number similar to "25", it
: can be any number, for example,
: Page 1 of 33
: Will your solution works ?

avatar
s*d
5
哦,简单说明一下:
r"Page\s+(\S+)\s+of\s+(\S+)"
里的"\s+"匹配空格,"\S+"匹配非空格,第一个(\S+)对应group(1),
第二个(\S+)对应group(2)。还有结尾的地方还可能要注意一下。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。