How to use ruby regexp to move xml tags in an xml document when they fall between ERB tags? -
i have word docx in document.xml file:
-<w:r><w:tab/> <w:t><%= @grantor </w:t></w:r> -<w:r w:rsidr="008e7a5b"> <w:t>1.upcase %>, manager </w:t></w:r>
i need take xml tags out in between erb tags (<%= , %>) , place them behind closing erb bracket (%>).
move this:
</w:t></w:r> -<w:r w:rsidr="008e7a5b"> <w:t>
to end with:
-<w:r><w:tab/> <w:t><%= @grantor1.upcase %> </w:t></w:r> -<w:r w:rsidr="008e7a5b"> <w:t>, manager </w:t></w:r>
i know not idea use regex's parsing xml, trivial. need loop through each set of erb tags, grab between first "<" , last ">" between erb tags, not rest of document...and move them behind %>. work fine because never put "<" or ">" in erb tags reason. of can't done in single regex, i'm not sure how go it. maybe regex /<%=.+%>/ (not sure how make multiline), loop through instances , run regex on each. i'm not sure how moving. newbie, appreciated.
i have word docx
that's first problem. deal plain text files.
but trivial
not true--at all.
i know not idea use regex's parsing xml
your text not valid xml anyway because contains < , > characters in content of tag, instance:
<w:t> <%= @grantor </w:t>
so whether xml parser can handle broken xml not certain.
here's more robust solution:
str = %q{ <%= "hello" %> <w:r> <w:tab/> <w:t><%= @grantor</w:t> </w:r> <w:r w:rsidr="008e7a5b"> <w:t>1.upcase %>, manager</w:t> </w:r> <%= "goodbye" %> <w:r> <w:tab/> <w:t><%= @grantor</w:t> </w:r> <w:r w:rsidr="008e7a5b"> <w:t>2.downcase %>, manager</w:t> </w:r> } str.gsub!(/<%= (.*?) %>/xms) |match| erb_contents = regexp.last_match[1] xml = "" if erb_contents.gsub!(/< .* >/xms, "") xml = regexp.last_match[0] end "<%=#{erb_contents}%>#{xml}" end puts str --output:-- <%= "hello" %> <w:r> <w:tab/> <w:t><%= @grantor1.upcase %></w:t> </w:r> <w:r w:rsidr="008e7a5b"> <w:t>, manager</w:t> </w:r> <%= "goodbye" %> <w:r> <w:tab/> <w:t><%= @grantor2.downcase %></w:t> </w:r> <w:r w:rsidr="008e7a5b"> <w:t>, manager</w:t> </w:r>
Comments
Post a Comment