Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up using the String.scan approach, the only tricky point there was figuring out that this returns an array of arrays, not a MatchData object, so there was some initial confusion on my part, mostly due to my ruby green-ness, but it's working as expected now. Also, I trimmed the regex per Trevoke's suggestion. But snake case? Never...;-) Anyway, here goes:</p> <pre><code>tagRegex = /(&lt;(?:webobject) (?:name)=(?:\w+\.)+(?:\w+)(?:&gt;))/i testFile = File.open('RegexTestingCompFix.txt', "r+") lineCount=0 testFile.each do |htmlLine| lineCount += 1 puts ("Current line: #{htmlLine} at line num: #{lineCount}") oldMatches = htmlLine.scan(tagRegex) #oldMatches thusly named due to not explicitly using Regexp or MatchData, as in "the old way..." if(oldMatches.size &gt; 0) oldMatches.each_index do |index| arrayMatch = oldMatches[index] aMatch = arrayMatch[0] #create a new regex using the match results; make sure to use auto escape method replacementRegex = Regexp.new(Regexp.escape(aMatch)) #replace any periods with underscores in a copy of lineMatchCapture periodToUnderscoreCorrection = aMatch.gsub(/\./, '_') #replace original match with underscore replaced copy within line, matching against the new escaped literal regex htmlLine.gsub!(replacementRegex, periodToUnderscoreCorrection) puts "The modified htmlLine is now: #{htmlLine}" end # I kind of still prefer the brackets...;-) end end </code></pre> <p>Now, why does MatchData work the way it does? It seems like it's behavior is a bug really, and certainly not very useful in general if you can't get it provide a simple means of accessing all the matches. Just my $.02</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload