新手问题 如何用正则表达式获取页数

hgq114 · April 12, 2013 · Last by small_fish__ replied at April 12, 2013 · 2312 hits

[ 总数:5, 共 1 页 ] 每页显示:20 50 100

想用正则获取到页数“5”,请达人指教

能不用 gem 吗?。。。。。。。。 杀鸡焉用牛刀。。。。

\d, 匹配出 5, 然后把,号截掉。

# encoding:UTF-8
require "rubygems"

string = "[ 总数: 5, 共 1 页 ] 每页显示: 20 50 100"
matched = /\d,/.match string
matchedStr = matched[0]
p matchedStr

可以用这个网站测试表达式: http://www.rubular.com/

#2 楼 @hgq114 涉及到分页的,现成的 gem,用起来很方便的

谢谢 1 楼和 3 楼的回复

string.split(',')[0].split(':')[1].strip

You need to Sign in before reply, if you don't have an account, please Sign up first.