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.
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.