正则表达式匹配具有多个属性的 xml 标签

Regex to match xml tag with multiple attributes

我正在尝试寻找可以匹配标签 <w:proofErr .... />.

的正则表达式

regex101 link: regex101

原字符串为:

<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:autoSpaceDE w:val="0"/><w:autoSpaceDN w:val="0"/><w:adjustRightInd w:val="0"/><w:spacing w:after="0" w:line="240" w:lineRule="auto"/><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr></w:pPr><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellStart"/><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="gramStart"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>student</w:t></w:r><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellEnd"/><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="gramEnd"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellStart"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>learning</w:t></w:r><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellEnd"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellStart"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>outcomes</w:t></w:r><w:proofErr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:type="spellEnd"/><w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidRPr="008D22B1"><w:rPr><w:rFonts w:cs="SerifGothicStd-Bold"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr><w:t>*</w:t></w:r><w:autoSpaceDE xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:val="0"/><w:autoSpaceDN xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:val="0"/>

我正在尝试使用以下正则表达式:

/<w:proofErr.+(?:\/>)/g

但是当我 运行 它只有一个大匹配,所有文本都从第一个 <w:prrofErr 开始并在字符串末尾结束。

如何使用正则表达式匹配每个 <w:proofErr .... />

您的正则表达式有效,但它会贪婪地将标记的开头与代表标记结尾的任何字符串匹配。基本上,就正则表达式而言,那个大蓝色组是一大 "tag"。

这是解决此问题的一种方法。尝试 this regex:

<w:proofErr[^>]+(?:"\/>)

它将 .* 替换为 [^>]*,这告诉它匹配任何字符 除了 右括号。​​