分页: 1 / 1

匹配字符串的问题

发表于 : 2007-02-19 13:24
hackem
最近在学Ruby,在里在有这样一个例子,是征服Ror的44页
< a href='http://www.baidu.com'>Welcome to baidu</a>
< a href='http://www.baidu.com'>Welcome to baidu</a>
< a href='http://www.baidu.com'>Welcome to baidu</a>
然后书本这样来匹配上面的内容

代码: 全选

<a [Hh][Rr][Ee][Ff]=[\'\"]?(.*?)[\'\"]?>(.*?)<\/a>
我对这是这样看的
<a [Hh][Rr][Ee][Ff]=[\'\"]
是匹配
< a href='

那么后面的
?
是哪来干什么用的呢
匹配
http://www.baidu.com直接用
.*
不就行了么?
为什么要用
.*?
??

发表于 : 2007-02-19 13:49
oneleaf
.*? 表示最小匹配.

发表于 : 2007-02-19 16:06
hackem

<a [Hh][Rr][Ee][Ff]=[\'\"]
后紧跟的

代码: 全选

?
又有什么作用呢?

发表于 : 2007-02-19 17:00
eexpress
这么麻烦,不如直接bash,grep或者awk。

发表于 : 2007-02-21 1:55
oneleaf
贪婪匹配限定符
A* Matches A 0 or more times (greedy)
A+ Matches A 1 or more times (greedy)
A? Matches A 1 or 0 times (greedy)
A{n} Matches A exactly n times (greedy)
A{n,} Matches A at least n times (greedy)

非贪婪匹配限定符
A*? Matches A 0 or more times (reluctant)
A+? Matches A 1 or more times (reluctant)
A?? Matches A 0 or 1 times (reluctant)

.* 表示匹配任意字符的多个,加上?号一起,就是说前面的匹配是非贪婪匹配,如果一旦后面的匹配有效,就直接匹配最下范围内的.*。

例如 abcdbcd a.*c 会匹配 abcdbc 而 a.*?c 会匹配 abc