Skip to content

Instantly share code, notes, and snippets.

@Yardboy
Created February 5, 2011 13:48
Show Gist options
  • Save Yardboy/812465 to your computer and use it in GitHub Desktop.
Save Yardboy/812465 to your computer and use it in GitHub Desktop.
I need some regex help...
Solved, with some help from StackOverflow that I apparently missed the first time around
(http://stackoverflow.com/questions/2013124/regex-matching-up-to-the-first-occurrence-of-a-character):
/<simpleChoice[^>]*>/
Match "<simpleChoice" plus any and all characters that do not match ">" followed by a ">".
[^>]* is a set of "zero or more characters that do not match >"
****************
I have this xml content (below), and I need to insert a "<content>" tag right after the <simpleChoice> tag using ruby gsub. I'm running into
trouble with the regex match due to the attributes in the <simpleChoice> tag. What I've tried is this:
gsub(/<simpleChoice.*>/, '\\0<content>')
But, the .* matches everything to the end of the xml, not just the end of that tag, so I end up with the original xml with a <content> tag
at the very end.
How do I prepare the regex to match the "<simpleChoice " text plus only the characters up to the first ">" that is encountered?
**********************
<assessmentItem xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p0 imsqti_v2p0.xsd" identifier="choice" adaptive="false" timeDependent="false" title="200535" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
<correctResponse>
<value>D</value>
</correctResponse>
</responseDeclaration>
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="integer">
<defaultValue>
<value>0</value>
</defaultValue>
</outcomeDeclaration>
<stylesheet href="stylesheet/scip.css" type="text/css"/>
<itemBody>
<span class="class1">
<b>The bee hummingbird is one of the world’s smallest birds. The average bee hummingbird weighs about 0.07 of an ounce. Which of the following is equivalent to 0.07?</b>
</span>
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
<simpleChoice identifier="A">
<span class="class1">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mfrac>
blahblahblah more xml...
</assessmentItem>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment