【正则】匹配头尾,并且中间不能有特定字符串
发布时间:2020-12-14 02:24:59 所属栏目:百科 来源:网络整理
导读:缘由: 开发Flex时调用到SWC文件的类库,想通过注释catalog.xml的script标签来屏蔽一部分类包,然后自己重写。但是由于标签过多,手动注释只会累死人,只能用正则替换来注释script标签。 替换内容: script name="comerror...... dep ....../ dep ......//s
缘由: 开发Flex时调用到SWC文件的类库,想通过注释catalog.xml的script标签来屏蔽一部分类包,然后自己重写。但是由于标签过多,手动注释只会累死人,只能用正则替换来注释script标签。 <script name="comerror......> <dep ....../> <dep ....../> </script> <script name="comwarn......> <dep ....../> <dep ....../> </script> <script name="comerror......> <dep ....../> <dep ....../> </script> 匹配逻辑: 1.以<script开头 2.以</script>结尾 3.中间必须包含name="comerror字串 4.中间不包含字串script
<(script)( name="com)(/error)(b((?!1).*?)+b)(.*?)(</1>)正则不解释了,匹配逻辑有所不同的请自行分析参考。 正确替换内容: <!--<$1$2$3$6$7--> 正确替换结果: <!--<script name="comerror......> <dep ....../> <dep ....../> </script>--> <script name="comwarn......> <dep ....../> <dep ....../> </script> <!--<script name="comerror......> <dep ....../> <dep ....../> </script>--> 之前我在网上找到很多的正则,都是有问题的。 错误替换结果: <!--<script name="comerror......> <dep ....../> <dep ....../> </script> <script name="comwarn......> <dep ....../> <dep ....../> </script> <script name="comerror......> <dep ....../> <dep ....../> </script>-->可以看到,上面匹配时把"comwarn"的script标签也注释掉了,因为这个正则匹配的只是头尾,中间没有排除掉script标签的单词,这是贪婪模式下的匹配结果。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |