怎样才能让我的正则表达不那么贪心?

时间:2011-05-17 13:10:23

标签: regex

我正在尝试选择以下XML的小型移植,但我的reg exp无法按预期工作。

我的注册表

<w:p [^<>]*><w:r><w:t>\[end participant\]<\/w:t><\/w:r><\/w:p>

它应匹配

<w:p w:rsidRDefault="009C141F" w:rsidP="006003BD">
  <w:pPr>
    <w:spacing w:after="240" />
  </w:pPr>
  <w:r>
    <w:t>[end participant]</w:t>
  </w:r>
</w:p>

但它匹配第一个w:p标签。

这是完整的XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
  <w:body>
    <w:p w:rsidRDefault="001660DB" w:rsidP="001660DB">
      <w:pPr>
        <w:pStyle w:val="Titre1" />
        <w:spacing w:before="0" />
      </w:pPr>
      <w:r>
        <w:t>Salut</w:t>
      </w:r>
    </w:p>
    <w:p w:rsidRDefault="001660DB" w:rsidP="001660DB">
      <w:pPr>
        <w:pStyle w:val="Titre2" />
        <w:spacing w:before="0" />
      </w:pPr>
      <w:r>
        <w:t>Hello les gens &amp;àè!</w:t>
      </w:r>
    </w:p>
    <w:p w:rsidRDefault="00F87349" />
    <w:p w:rsidRDefault="009C141F">
      <w:r>
        <w:t>[start participant]</w:t>
      </w:r>
    </w:p>
    <w:p w:rsidRDefault="009C141F" w:rsidP="006003BD">
      <w:pPr>
        <w:pStyle w:val="Titre1" />
        <w:spacing w:before="0" w:after="240" />
      </w:pPr>
      <w:r>
        <w:t xml:space="preserve">Mr #NOM# #PRENOM#</w:t>
      </w:r>
    </w:p>
    <w:p w:rsidRDefault="009C141F" w:rsidP="006003BD">
      <w:pPr>
        <w:spacing w:after="240" />
      </w:pPr>
      <w:r>
        <w:t>[end participant]</w:t>
      </w:r>
    </w:p>
    <w:sectPr w:rsidSect="00425138">
      <w:pgSz w:w="11906" w:h="16838" />
      <w:pgMar w:top="1417" w:right="1417" w:bottom="1417" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />
      <w:cols w:space="708" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:body>
</w:document>

感谢

1 个答案:

答案 0 :(得分:0)

我认为您错过了spacing的标记:

<w:p [^<>]*><w:pPr><w:spacing [^<>]*/></w:pPr><w:r><w:t>\[end participant\]<\/w:t><\/w:r><\/w:p>