使用GREP替换所有匹配的字符串

时间:2015-10-14 19:36:45

标签: regex grep textwrangler

我正在使用TextWrangler从XML获取特定信息。我需要找到一个存在的文件名列表,并打印出那些文件名。

代码示例如下:

<file id="file_1045280">
    <name>SKY_A026C032_150707_R4RO.mov</name>
    <pathurl>file://localhost/M:/FPL_MEDIA/04_MEZZANINE/SKY/SKY-EP03/SKY-0312_20150707_AA_A026/SKY_A026C032_150707_R4RO.mov</pathurl>
    <duration>1796</duration>
    <timecode>
        <rate>
            <ntsc>false</ntsc>
            <timebase>25</timebase>
        </rate>
        <frame>0</frame>
        <displayformat>NDF</displayformat>
    </timecode>
    <media>
        <video>
            <duration>1796</duration>
            <samplecharacteristics>
                <width>1920</width>
                <height>1080</height>
            </samplecharacteristics>
        </video>
    </media>
</file>
                            <sourcetrack>
                                <mediatype>video</mediatype>
                            </sourcetrack>
                            <link>
                                <linkclipref>clipItem_1045280</linkclipref>
                                <mediatype>video</mediatype>
                                <trackindex>1</trackindex>
                            </link>
                        </clipitem>
                        <enabled>TRUE</enabled>
                        <locked>FALSE</locked>
                    </track>
                </video>
            </media>
        </clip>
        <clip id="clip_1045282">
            <name>SKY_A026C018_150707_R4RO</name>
            <duration>958</duration>
            <rate>
                <ntsc>false</ntsc>
                <timebase>25</timebase>
            </rate>
            <in>-1</in>
            <out>-1</out>
            <masterclipid>clip_1045282</masterclipid>
            <ismasterclip>TRUE</ismasterclip>
            <media>
                <video>
                    <track>
                        <clipitem id="clipitem_1045282">
                            <name>SKY_A026C018_150707_R4RO</name>
                            <duration>958</duration>
                            <masterclipid>clip_1045282</masterclipid>
                            <rate>
                                <ntsc>false</ntsc>
                                <timebase>25</timebase>
                            </rate>
                            <in>0</in>
                            <out>958</out>
                            <start>0</start>
                            <end>958</end>
<file id="file_1045282">
    <name>SKY_A026C018_150707_R4RO.mov</name>
    <pathurl>file://localhost/M:/FPL_MEDIA/04_MEZZANINE/SKY/SKY-EP03/SKY-0312_20150707_AA_A026/SKY_A026C018_150707_R4RO.mov</pathurl>
    <duration>958</duration>
    <timecode>
        <rate>
            <ntsc>false</ntsc>
            <timebase>25</timebase>
        </rate>
        <frame>0</frame>
        <displayformat>NDF</displayformat>
    </timecode>
    <media>
        <video>
            <duration>958</duration>
            <samplecharacteristics>
                <width>1920</width>
                <height>1080</height>
            </samplecharacteristics>
        </video>
    </media>
</file>
                            <sourcetrack>
                                <mediatype>video</mediatype>
                            </sourcetrack>
                            <link>
                                <linkclipref>clipItem_1045282</linkclipref>
                                <mediatype>video</mediatype>
                                <trackindex>1</trackindex>
                            </link>
                        </clipitem>
                        <enabled>TRUE</enabled>
                        <locked>FALSE</locked>
                    </track>
                </video>
            </media>
        </clip>
        <clip id="clip_1045283">
            <name>SKY_A026C033_150707_R4RO</name>
            <duration>1202</duration>
            <rate>
                <ntsc>false</ntsc>
                <timebase>25</timebase>
            </rate>
            <in>-1</in>
            <out>-1</out>
            <masterclipid>clip_1045283</masterclipid>
            <ismasterclip>TRUE</ismasterclip>
            <media>
                <video>
                    <track>
                        <clipitem id="clipitem_1045283">
                            <name>SKY_A026C033_150707_R4RO</name>
                            <duration>1202</duration>
                            <masterclipid>clip_1045283</masterclipid>
                            <rate>
                                <ntsc>false</ntsc>
                                <timebase>25</timebase>
                            </rate>
                            <in>0</in>
                            <out>1202</out>
                            <start>0</start>
                            <end>1202</end>

目前,我正在使用以下Grep:

.*?(\<name\>)(.*)(.mov).*

这设法找到我需要的字符串。但是,我需要替换所有剩余的文本,所以我留下了一个文件名列表。

有人可以建议我如何解决这个问题吗?

提前致谢, 马特

2 个答案:

答案 0 :(得分:0)

使用TextWrangler,一种快速的方法是首次使用 - &gt; 文字 - &gt; 包含... 的处理行,以 Grep 复制到新文档搜索<name>.+\.mov</name>。 生成的文件可以清除,搜索({1}}的某些内容,并在选中 Grep 的情况下替换为^\s*<name>(.+\.mov)</name>\s*$

答案 1 :(得分:-1)

这个怎么样?这有点重叠,但这意味着

"match everything as if it's a single line that 
[comes after </name> and before <name>], or 
[is between the beginning and <name>] or 
[is the <name> or </name> tags itself].

(?ms)(?<=<\/name>)(.*?)(?=<name>)|(^.*?<name>)|(<.?name>)

https://regex101.com/r/vV4xZ6/2