在shell里怎么得到正则匹配的部分啊

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

在shell里怎么得到正则匹配的部分啊

#1

帖子 hellojinjie » 2009-03-29 11:38

代码: 全选

#noticep{ margin:0 10px; width:660px; background:url(http://mobile.qq.com:8080/web_mobile/mobile_res/mobile2009/images/double_line.gif) repeat-x bottom; padding-bottom:20px; }
比如上面这是一行,我要得到图片连接部分。

另外,在sed grep awk 里有环视的吗
Say hello to everyday!
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 在shell里怎么得到正则匹配的部分啊

#2

帖子 HuntXu » 2009-03-29 12:44

代码: 全选

echo "#noticep{ margin:0 10px; width:660px; background:url(http://mobile.qq.com:8080/web_mobile/mobile_res/mobile2009/images/double_line.gif) repeat-x bottom; padding-bottom:20px; }" |sed -r "s|.+(http://[A-Za-z0-9_./:]+).+|\1|g"
这样子的么?
HUNT Unfortunately No Talent...
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 在shell里怎么得到正则匹配的部分啊

#3

帖子 hellojinjie » 2009-03-29 20:45

:em21 :em21

我刚刚手工把链接改了。好大的工程啊。。 :em20 :em20
Say hello to everyday!
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 在shell里怎么得到正则匹配的部分啊

#4

帖子 hellojinjie » 2009-03-29 20:51

sed -r "s|.+(http://[A-Za-z0-9_./:]+).+|\1|g"
HuntXu, 你写的sed我有点不太懂,可以帮我解释下吗。
第一处,| 竖线是什么?书上讲的是/斜线啊,是不是当有 -r 时要用竖线
第二处,\1是什么啊,是指前面匹配的第一的括号内的东西的吗?
Say hello to everyday!
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 在shell里怎么得到正则匹配的部分啊

#5

帖子 HuntXu » 2009-03-29 23:52

1.竖线和斜线一样,分割符而已...因为链接里有斜线,如果用斜线做分割符会有一堆转义的"\/"...
2.是
HUNT Unfortunately No Talent...
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 在shell里怎么得到正则匹配的部分啊

#6

帖子 hellojinjie » 2009-03-30 8:57

thx :em01
Say hello to everyday!
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: 在shell里怎么得到正则匹配的部分啊

#7

帖子 xiooli » 2009-03-30 9:23

这样不是更简单么?

代码: 全选

echo "#noticep{ margin:0 10px; width:660px; background:url(http://mobile.qq.com:8080/web_mobile/mobile_res/mobile2009/images/double_line.gif) repeat-x bottom; padding-bottom:20px;}"|awk -F'[()]' '{print $2}'
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 在shell里怎么得到正则匹配的部分啊

#8

帖子 hellojinjie » 2009-03-30 9:32

原来awk的分割符还可以是这样的。

可以是正则表达式的吗,试试
Say hello to everyday!
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 在shell里怎么得到正则匹配的部分啊

#9

帖子 aerofox » 2009-03-31 5:51

代码: 全选

grep -o "http://[^)]*"
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

Re: 在shell里怎么得到正则匹配的部分啊

#10

帖子 hellojinjie » 2009-03-31 16:22

aerofox 写了:

代码: 全选

grep -o "http://[^)]*"
原来grep还有o这个选项,受教了,thanks
Say hello to everyday!
回复