我正在尝试进行多行sed范围匹配.我的意思是,范围的开头本身就是多行.来源看起来像这样 filter-mapping filter-nameCache Filter - Resource/filter-name url-pattern*.css/url-pattern/filter-mappingfilter-mapping fi
<filter-mapping> <filter-name>Cache Filter - Resource</filter-name> <url-pattern>*.css</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Cache Filter - Resource</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Cache Filter - Resource</filter-name> <url-pattern>*.js</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Cache Filter - Resource JSP</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
并且唯一唯一识别段开始范围是整个前三行,并且范围的结束由整个最后两行标识.
有没有办法指定多线模式来开始范围匹配?
好吧,所以我选择了tchrist的解决方案并使用了perl.这是我的正则表达式的样子(在这个例子中,我正在评论xml).我也在做一个内联替换文件.
perl -0777 -pi -e 's|([\t ]*<filter-mapping>\s*<filter-name>Cache Filter - Resource</filter-name>\s*<url-pattern>\*\.css[^\0]*<filter-name>Cache Filter - Resource JSP</filter-name>\s*<url-pattern>\*\.jsp</url-pattern>\s*</filter-mapping>)|<!-- \1 -->|g' inputfile