0

I am trying to write a regex to find a selection in one long string of xml, and pull out the first instance of a tag it finds.

My current code (which pulls out every match): (<action>(.*?)</action>)

Sample code:

<request guid="58004a1e-0c32-4002-a101-59fc28af5836">
<action>Sample/Action</action><date>"7/12/12"</date>

I have tried using multiple ways I have found to either match the first instance and disregard everything else, check and make sure there is no match before the current match, set it to match exactly one, etc. but they all return two matches. I am unable to set the regex to look for a particular leading or trailing tag as those can change.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
wgallon
  • 57
  • 4
  • 3
    Why don't you use an xml parser instead of regex? – L.B Jul 12 '12 at 15:43
  • You write that you can get *all* matches, then it should be simple to get the first one? – dacwe Jul 12 '12 at 15:49
  • Sounds to me like you need to re-think your data structure/parsing. Better getting it right now, rather than fixing it down the line when you realize its not suitable to your evolving needs. – Phil Jul 12 '12 at 15:53

1 Answers1

2

Simple answer: stop.

You're asking the wrong question, because you've decided you're going to use regular expressions to solve your problem before working out whether regular expressions are an appropriate way to solve your problem.

Both C# and Java have XML parsing libraries, and you'd be much better off using one of those, rather than trying to duplicate their functionality using regular expressions, a task which is in general impossible, as XML is not a regular language.

See also this answer, although that question was about HTML.

Community
  • 1
  • 1
Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • The issue being I'm not using this for an application I'm writing, I'm using it for another application for our company that is running in java and the only thing I am able to do is pass it the regex string. I had assumed that there was no realistic way to do this, but I had wanted to research it anyway just to make sure. – wgallon Jul 12 '12 at 15:56
  • 1
    @BillyGallon pass a regex that selects the whole XML and run an XPath query against the result. It shouldn't be hard to select the first match from a collection returned by your current regexp either. – toniedzwiedz Jul 12 '12 at 15:59