-2

I cannot parse the xml because at this stage it is invalid and will not parse.

How would I match a simple xml element (with the id) like so:

<g id="crop"></g>

Using a regular expression?

Thanks

Community
  • 1
  • 1
tmutton
  • 1,091
  • 7
  • 19
  • 42
  • 6
    Why with a regex? Use Xml tools for Xml. – H H Sep 24 '12 at 13:41
  • Yes but in this case I would like to use regular expression – tmutton Sep 24 '12 at 13:43
  • I cannot parse the xml because at this stage it is invalid and will not parse. This is why I am looking for RegEx. – tmutton Sep 24 '12 at 13:45
  • 2
    "Would like to" is nonsense, a waste of everybody's time. Maybe you need to but then this sample is probably too simple, any RegEx solution will probably not scale very well. – H H Sep 24 '12 at 13:46
  • Maybe better to post how/why it is invalid and ask how to repair it. – H H Sep 24 '12 at 13:48
  • 1
    @codemonkey an invalid xml can be in many forms, I don't think you can find *magic* regex to fix all of them. How about posting some sample of broken xmls – L.B Sep 24 '12 at 13:51
  • Is the element guaranteed to be empty (as in your example)? Or it can contain arbitrary, possibly-invalid XML? – Tim Goodman Sep 24 '12 at 13:55
  • the element (g) will always contain more elements. – tmutton Sep 24 '12 at 13:57
  • You're unlikely to get an answer that avoids [this problem](http://stackoverflow.com/a/1732454/). Note that Html is Xml in this context. – H H Sep 24 '12 at 13:59
  • As mentioned [here](http://stackoverflow.com/a/1732454/82682) you CAN'T parse XML using regex. You just can't. Period. – joce Apr 02 '13 at 04:44

1 Answers1

4

At the risk of a downvote... How about using the right tool for the job?

var xmlStr = "<g id=\"crop\"></g>";
var att = XElement.Parse(xmlStr).Attribute("id");
var id = (string)att;
spender
  • 117,338
  • 33
  • 229
  • 351
  • This is the right tool for the job but in my circumstance I cannot use it as the xml will not parse. I need to fix the xml first before I put it through a parser. – tmutton Sep 24 '12 at 13:47
  • ...although I've found myself in exactly the same situation... discogs, I'm looking at you! – spender Sep 24 '12 at 13:49