Multiple substitutions in one pass

C

Christopher Causer

I have a long document which I am trying to parse. I am coming unstuck
trying to speed it up and get all the substitutions out the way quickly.
I have a hash array of original and new things to substitute, and it
works. However, I'm sure there is a more efficient way of doing things.
RegExp has a union method, but that doesn't seem to handle string
capture within matches.

One final thing is that I would like this sub to throw something if no
substitutions are performed on the str.


--------------------------------------------------
def makeSubs(str)
subCurlyArray={/section%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+\2',
/subsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---++\2',
/subsubsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+++\2',
/emph%LCURL%(\d+)(.*?)%RCURL%\1/ => '<em>\2</em>',
/includegraphics\[(.*)\]%LCURL%(\d+)(.*?)%RCURL%\1/ =>'IMAGE <img
src="\3" \2> ENDIMAGE',
/texttt%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2 </verbatim>',
/code%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2</verbatim>',
/textbf%LCURL%(\d+)(.*?)%RCURL%\1/ => '*\2*',
/textit%LCURL%(\d+)(.*?)%RCURL%\1/ => '_\2_',
}

subArray.each do |orig, new|
str.gsub!(orig, new)
else


return str

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top