-4

What I need is something like this:

/<[\w\d]+ ([\w\d]+\=[w\d])+\/>/

Something that would match several attribute key/value pairs. Is that possible?

3
  • 6
    <![CDATA[ <I am="not" xml="tag"/> ]]> <!-- <me:neither /> --> Commented Mar 11, 2010 at 16:50
  • 5
    stackoverflow.com/questions/1732348/… Commented Mar 11, 2010 at 17:50
  • 2
    @Ether: Actually you still can parse XML with Perl regular expressions, since Perl regular expressions are not regular either ;) But it is a task to accomplish. Commented Mar 12, 2010 at 15:12

2 Answers 2

10

You'll have much more succes using an XML parser, for example, XML::Parser. Parsing XML using regular expressions is very difficult (impossible?) and unless your use case is trivial, a proper XML parser is the reliable solution.

Sign up to request clarification or add additional context in comments.

1 Comment

Impossible without a question mark. XML is not a regular language. Of course, some implementations of 'regular expressions' actually match non-regular languages, but that's another matter.
6

It may be possible for a very limited range of valid XML. If you are trying to parse XML, I strongly recommend that you use a parser, rather than try to use regular expressions. XML::Twig is a good general-purpose parser available from CPAN.

For further reading, see also: XML parsing vs Regular expressions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.