Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:47:15+00:00 2026-05-13T16:47:15+00:00

Ruby 1.9.1, OSX 10.5.8 I’m trying to write a simple app that parses through

  • 0

Ruby 1.9.1, OSX 10.5.8

I’m trying to write a simple app that parses through of bunch of java based html template files to replace a period (.) with an underscore if it’s contained within a specific tag. I use ruby all the time for these types of utility apps, and thought it would be no problem to whip up something using ruby’s regex support. So, I create a Regexp.new… object, open a file, read it in line by line, then match each line against the pattern, if I get a match, I create a new string using replaceString = currentMatch.gsub(/./, ‘_’), then create another replacement as whole string by newReplaceRegex = Regexp.escape(currentMatch) and finally replace back into the current line with line.gsub(newReplaceRegex, replaceString) Code below, of course, but first…

The problem I’m having is that when accessing the indexes within the returned MatchData object, I’m getting the first result twice, and it’s missing the second sub string it should otherwise be finding. More strange, is that when testing this same pattern and same test text using rubular.com, it works as expected. See results here

My pattern:

(<(?:WEBOBJECT|webobject) (?:NAME|name)=(?:[a-zA-Z0-9]+.)+(?:[a-zA-Z0-9]+)(?:>))

Text text:

<WEBOBJECT NAME=admin.normalMode.someOtherPatternWeDontWant.moreThatWeDontWant>moreNonMatchingText<WEBOBJECT NAME=admin.SecondLineMatch>AndEvenMoreNonMatchingText

Here’s the relevant code:

tagRegex = Regexp.new('(<(?:WEBOBJECT|webobject) (?:NAME|name)=(?:[a-zA-Z0-9]+\.)+(?:[a-zA-Z0-9]+)(?:>))+')  

testFile = File.open(‘RegexTestingCompFix.txt’, “r+”)
lineCount=0
testFile.each{|htmlLine|
lineCount += 1
puts (“Current line: #{htmlLine} at line num: #{lineCount}”)
tagMatch = tagRegex.match(htmlLine)
if(tagMatch)

  matchesArray = tagMatch.to_a  
  firstMatch = matchesArray[0]  
  secondMatch = matchesArray[1]  
  puts "First match: #{firstMatch} and second match #{secondMatch}"  
  tagMatch.captures.each {|lineMatchCapture|  
    puts "Current capture for tagMatches: #{lineMatchCapture} of total match count #{matchesArray.size}"  
    #create a new regex using the match results; make sure to use auto escape method  
    originalPatternString = Regexp.escape(lineMatchCapture)  
    replacementRegex = Regexp.new(originalPatternString)  
    #replace any periods with underscores in a copy of lineMatchCapture  
    periodToUnderscoreCorrection = lineMatchCapture.gsub(/\./, '_')  
    #replace original match with underscore replaced copy within line  
    htmlLine.gsub!(replacementRegex, periodToUnderscoreCorrection)  
    puts "The modified htmlLine is now: #{htmlLine}"    
    }  
end  

}

I would think that I should get the first tag in matchData[0] then the second tag in matchData1, or, what I’m really doing because I don’t know how many matches I’ll get within any given line is matchData.to_a.each. And in this case, matchData has two captures, but they’re both the first tag match

which is: <WEBOBJECT NAME=admin.normalMode.someOtherPatternWeDontWant.moreThatWeDontWant>

So, what the heck am I doing wrong, why does rubular test give me the expected results?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:47:16+00:00Added an answer on May 13, 2026 at 4:47 pm

    You want to use the on String#scan instead of the Regexp#match:

    tag_regex = /<(?:WEBOBJECT|webobject) (?:NAME|name)=(?:[a-zA-Z0-9]+\.)+(?:[a-zA-Z0-9]+)(?:>)/
    
    lines = "<WEBOBJECT NAME=admin.normalMode.someOtherPatternWeDontWant.moreThatWeDontWant>moreNonMatchingText\
         <WEBOBJECT NAME=admin.SecondLineMatch>AndEvenMoreNonMatchingText"
    
    lines.scan(tag_regex)
    # => ["<WEBOBJECT NAME=admin.normalMode.someOtherPatternWeDontWant.moreThatWeDontWant>", "<WEBOBJECT NAME=admin.SecondLineMatch>"]
    

    A few recommendations for next ruby questions:

    • newlines and spaces are your friends, you don’t loose points for using more lines on your code 😉
    • use do-end on blocks instead of {}, improves readability a lot
    • declare variables in snake case (hello_world) instead of camel case (helloWorld)

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to install the cairo ruby gem on OSX 10.6. There seems
I am developing a ruby on rails app on OSX and I am logged
I found out that I have two versions of ruby installed on OSX 10.6.2
After, reading and understanding Dan Benjamin's post about installing Ruby, Rails, etc. on OSX
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Ruby on Rails has magic timestamping fields that are automatically updated when a record
I'm trying to use rvm to install Ruby 1.9.2 on my Mac running Snow
I'm running ruby on Mac OSX 10.6.4, using Ruby 1.8 and activerecord 2.3.8. I'm
I'm having an issue compiling (make) ruby on Mac Osx . I get the
I need to run a ruby client that wakes up every 10 minutes, takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.