Regex for xml parsing

Posted by ogmios on Stack Overflow See other posts from Stack Overflow or by ogmios
Published on 2011-03-05T13:15:07Z Indexed on 2011/03/05 15:24 UTC
Read the original article Hit count: 174

Filed under:
|
|

What is your opinon about following regexes - is it correct?

  1. To find element with spcific and required attribute

"<(" + elem_name + ")(\s+(?:[^<>]?\s+)" + attr_name + "\s*=\s*(['\"])((?:(?!\3).))\3[^<>])>(.*?)"

  1. To find element with spcific but optional attribute

"<(" + elem_name + ")(\s*>|\s+(?:[^<>]?\s+)(?:" + attr_name + "\s*=\s*(['\"])((?:(?!\3).))\3)?[^<>])>(.*?)"

Pleas not another answer "use existing xml parser". Question is - are the regexes proper or not? This is specific situation - C language in embedded system and xml is not well-formed (cannot be fixed - does not depend on me). Xml have specified schema and no problem with namespaces etc. exists.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about regex