匹配字符串的问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
hackem
帖子: 258
注册时间: 2006-10-19 21:42
来自: inside
联系:

匹配字符串的问题

#1

帖子 hackem » 2007-02-19 13:24

最近在学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直接用
.*
不就行了么?
为什么要用
.*?
??
ubuntu是个好东西
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#2

帖子 oneleaf » 2007-02-19 13:49

.*? 表示最小匹配.
头像
hackem
帖子: 258
注册时间: 2006-10-19 21:42
来自: inside
联系:

#3

帖子 hackem » 2007-02-19 16:06


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

代码: 全选

?
又有什么作用呢?
ubuntu是个好东西
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2007-02-19 17:00

这么麻烦,不如直接bash,grep或者awk。
● 鸣学
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#5

帖子 oneleaf » 2007-02-21 1:55

贪婪匹配限定符
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
回复