Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用RegExp获取实际的XML标记_Xml_Regex - Fatal编程技术网

使用RegExp获取实际的XML标记

使用RegExp获取实际的XML标记,xml,regex,Xml,Regex,我有一个XML,看起来像: <drawing><some other tags><Picture><some other tags></drawing><drawing><some other tags><Chart><some other tags></drawing> 我想提取 <drawing><some other tags><Cha

我有一个XML,看起来像:

<drawing><some other tags><Picture><some other tags></drawing><drawing><some other tags><Chart><some other tags></drawing>

我想提取

<drawing><some other tags><Chart><some other tags></drawing>

目前我正在使用此RegExp:

/<drawing>.*?<Chart>.*?</drawing>/g 
/.*?*?/g
但是,它会返回整个XML,因为它也是有效的。但我只想要第二次发生,无法得出解决方案。
提前感谢。

关于使用正则表达式解析xml的所有免责声明,如果您想要正则表达式解决方案,请使用以下内容:

<drawing>(?:(?!</drawing>).)*?<Chart>.*?</drawing>
(?:(?!)*?*?
查看中的匹配

解释

  • 匹配文字字符
  • (?:(?!)
    匹配一个没有开头的字符
  • *?
    懒洋洋地重复此匹配,直到
  • 匹配文字字符
  • *?
    惰性地匹配字符直到

不要用正则表达式解析xml。稍微有点建设性(希望如此),您的正则表达式引擎是哪种风格?不幸的是,我正在继续一个这样做的项目。谢谢,它成功了!我尝试了非捕获和负前瞻,但不是同时进行。但请注意,它有时会失败,例如,如果某些字符串出现在注释或CDATA节中,或者文档在允许但不允许的位置使用空格