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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2012-Apr-23 17:28 UTC
Re: Regular expressions Multiple matches in the same line
Take a look at String#scan for multiple matches. Walter On Apr 23, 2012, at 12:07 PM, Loren <loren.boros-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.