使用perl用单词替换多行

时间:2015-06-15 15:34:24

标签: regex

[root@localhost test]# perl -e 'undef $/;while (<>) {s/\<|[a-zA-Z]|\>|\//one/gs;print}' file


oneoneoneoneoneoneoneoneoneoneoneone

oneoneoneoneoneoneoneoneoneoneoneone

oneoneoneoneoneoneoneoneoneoneoneone

我只需要一次打印

[root @ localhost test] #cat file.input

                   <tag>X</tag>
                   <tag>Y</tag>
                   <tag>Z</tag>

[root @ localhost test] #cat file.output

                    one

[root @ localhost test]#

1 个答案:

答案 0 :(得分:0)

以下是一些解释:

 <                             # Opening tag
 ( \w+ )                       # (1), tag name
 \s*                           # Whitespaces: tab,space,newlines, etc.. 
 > [^>]*? </ \1 \s* >          # Some text then Closing tag
 (?:                           # Cluster group
      \s*                           # Whitespaces: tab,space,newlines, etc..
      <                             # Repeat as above, with tag name
      \1                            #    ""
      \s*                           #    ""
      > [^>]*? </ \1 \s* >          #    ""
 )*                            # End cluster, do 0 to many times