Hi,

Could somebody please show me a way to find, and display multiple
regex matches in the same line?
Like this:

test_string = "I shot the Sherrif but I didn't shot the Deputy."

# The word I would like to find is the "shot" and displayed like
"shotshot" or like each match could go into a new #line like:
"shot"
"shot"
# Some ways I tried:

regex = /shot/ =~ test_string
puts $& ------------> # would only match the
first match
regex = /(shot)*/ =~ test_string
puts $& -------------> # only applys repetition
if the word appears next to each other with no spaces
# in between like
"shotshot". "shot shot" or "shot (some text) shot" would fail.

puts regex = test_string.grep/shot/ ------------> # From Ruby 1.9
String are not enumerable therfore it would give
# a
"no method error".

I could solve the problem without using regexes but I would like to
know how to solve it with regex.
I hope I was clear enough in describing the problem. I am new to
programming, and ruby is my first language.


Kind Regards,

Loren

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Loren at Apr 23, 2012 at 2:42 pm
    Hi,

    Could somebody please show me a way to find, and display multiple
    regex matches in the same line?
    Like this:

    test_string = "I shot the Sherrif but I didn't shot the Deputy."

    The word I would like to find is the "shot" and displayed like
    "shotshot" or like each match could go into a new line like:
    "shot"
    "shot"
    Some ways I tried:

    regex = /shot/
    regex =~ test_string
    puts $& ------------> # would only match the first match

    regex = /(shot)*/
    regex =~ test_string
    puts $& -------------> # only applys repetition if the word appears
    next to each other with no spaces in between like
    "shotshot". "shot shot" or "shot (some text) shot" would fail.

    puts regex = test_string.grep/shot/ ------------> # From Ruby 1.9
    String are not enumerable therfore it would give a "no method error".

    I could solve the problem without using regexes but I would like to
    know how to solve it with regex.
    I hope I was clear enough in describing the problem. I am new to
    programming, and ruby is my first language.

    Kind Regards,

    Loren

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Billy Alcazar at Apr 23, 2012 at 3:31 pm
    Please can some one show me how to stop getting emails from
    list@ruby-forum.com

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Scott Ribe at Apr 23, 2012 at 3:35 pm

    On Apr 23, 2012, at 9:31 AM, Billy Alcazar wrote:

    Please can some one show me how to stop getting emails from
    list@ruby-forum.com
    There are instructions at the bottom of every email you receive. Did you try them?

    --
    Scott Ribe
    scott_ribe@elevated-dev.com
    http://www.elevated-dev.com/
    (303) 722-0567 voice




    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Loren at Apr 23, 2012 at 4:58 pm
    Well, sorry but I would appreciate if you would post your own question
    instead of highjacking mine.
    On Apr 23, 4:31 pm, Billy Alcazar wrote:
    Please can some one show me how to stop getting emails from
    l...@ruby-forum.com

    --
    Posted viahttp://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 23, '12 at 2:30p
activeApr 23, '12 at 4:58p
posts5
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase