正则表达式

匹配html的嵌入代码 [code:1:42c27db477]<[^>]*>[/code:1:42c27db477]

匹配[....]的嵌入码[code:1:42c27db477] \[[^]]\{1,\}\][/code:1:42c27db477]

删除仅由空字符组成的行
[code:1:42c27db477]sed '/^[[:space:]]*$/d' filename[/code:1:42c27db477]

匹配html标签
[code:1:42c27db477]/\(<[^>]*>\)/[/code:1:42c27db477]例如:从html文件中剔除html标签
[code:1:42c27db477]sed 's/\(<[^>]*>\)//g;/^[[:space:]]*$/d'  file.html[/code:1:42c27db477]

例如:要从下列代码中去除"[]"及其中包括的代码
[code:1:42c27db477][b:4c6c2a6554][color=red:4c6c2a6554]一. 替换[/color:4c6c2a6554][/b:4c6c2a6554]
sed 's/\[[^]]\{1,\}\]//g' filename[/code:1:42c27db477]

[b:42c27db477]匹配日期:[/b:42c27db477]
[code:1:42c27db477]Month, Day, Year [A-Z][a-z]\{3,9\}, [0-9]\{1,2\}, [0-9]\{4\}
2003-01-28 或 2003.10.18 或 2003/10/10 或 2003 10 10
\([0-9]\{4\}[ /-.][0-2][0-9][ /-.][0-3][0-9]\)[/code:1:42c27db477]
[b:42c27db477]匹配IP地址[/b:42c27db477]
[code:1:42c27db477]\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)
\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)[/code:1:42c27db477]

[b:42c27db477]匹配数字串[/b:42c27db477]
[code:1:42c27db477][-+]*[0-9]\{1,\} 整数
[-+]*[0-9]\{1,\}\.[0-9]\{1,\}  浮点数[/code:1:42c27db477]

[b:42c27db477]从字串中解析出两个子串(前2各字符和后9个字符) [/b:42c27db477]
[code:1:42c27db477]echo "WeLoveChinaUnix"|sed -e 'H;s/\(..\).*/\1/;x;s/.*\(.\{9\}\)$/\1/;x;G;s/\n/ /' 
We ChinaUnix [/code:1:42c27db477]

[b:42c27db477]分解日期串[/b:42c27db477] 
[code:1:42c27db477]echo 20030922|sed 's/\(....\)\(..\)\(..\)/\1 \2 \3/'|read year month day 
echo $year $month $day [/code:1:42c27db477]

[b:42c27db477]文件内容倒序输出[/b:42c27db477]
[code:1:42c27db477]sed '1!G;h;$!d'  oldfile >newfile[/code:1:42c27db477]

posted on 2004-09-28 11:32  feeling  阅读(1840)  评论(1编辑  收藏  举报

导航