# Multiple substitutions in one pass

**URL:** <https://www.ruby-forum.com/t/multiple-substitutions-in-one-pass/130231>\
**Category:** Ruby\
**Created:** [February 26, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/multiple-substitutions-in-one-pass/130231 "2008-02-26T16:24:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscauser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/65b543/32.png) [@chriscauser](https://www.ruby-forum.com/u/chriscauser)\
**Post date:** [February 26, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/multiple-substitutions-in-one-pass/130231/1 "2008-02-26T16:24:02Z")

</div>

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/ =\> ‘_\2_’,  
/includegraphics[(._)]%LCURL%(\d+)(._?)%RCURL%\1/ =\>‘IMAGE \<img  
src="\3" \2\> ENDIMAGE’,  
/texttt%LCURL%(\d+)(._?)%RCURL%\1/ =\> '\2 ',  
/code%LCURL%(\d+)(._?)%RCURL%\1/ =\> ‘\2’,  
/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

Thank you
