免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1771 | 回复: 5
打印 上一主题 下一主题

正则表达的中的存储器 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-19 22:36 |只看该作者 |倒序浏览
在原版的《linux shell s by example》中
$s/\([0o]ccur\)ence/\1rence/

解释:The editor searches for the entire string occurence or Occurence (note: the words are misspelled), and if found, the pattern portion enclosed in parentheses is tagged (i.e., either occur or Occur is tagged). Since this is the first pattern tagged, it is called tag 1. The pattern is stored in a memory register called register 1. On the replacement side, the contents of the register are replaced for \1 and the rest of the word rence, is appended to it. We started with "occurence" and ended up with "occurrence."

说是标签1中被保存在寄存器1中,把\1中的内容替换,occurence开始,以occurrence结束,谁能详细解释下各部分的用法
还有这个
s/\(square\) and \(fair\)/\2 and \1/

标签2的寄存器存了fair,标签1寄存器存了square,\2为fair,\1为fair,匹配square and fair,不知道这样了解是否对?

论坛徽章:
0
2 [报告]
发表于 2008-03-19 23:09 |只看该作者
上面\1 = square ; \2 = fair。寄存器最多可以有9个,依次对应相应的\(*\)里面的*。
这些在shell下面跑一次就有印象了

论坛徽章:
0
3 [报告]
发表于 2008-03-19 23:16 |只看该作者

回复 #2 galford433 的帖子

谢谢,发现这种正则表达式都有一个规律以s/开头,中间用/连接,比如s/\(square\) and \(fair\)/\2 and \1/,有谁能详细总结下用法。

论坛徽章:
0
4 [报告]
发表于 2008-03-19 23:23 |只看该作者
s/part/repl/
是sed里的命令,正则表达式可以在其中使用而已

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2008-03-19 23:32 |只看该作者

回复 #3 wgqjjq 的帖子

看置顶十二篇之sed

论坛徽章:
0
6 [报告]
发表于 2008-03-20 08:57 |只看该作者
s///是sed的应用,可以用正则,也可以不用,正则也可以是/re/d,正则和s///没有关系。建议去看看man sed和info sed。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP