-1

I want to mimic

while($string=~/($regular_expression)/g){
  print $1."\n";
}

of Perl in Ruby.

Is there a way to do that in Ruby (e.g. print something per match)?

user3477465
  • 183
  • 2
  • 2
  • 6

1 Answers1

1

To print each match in a string:

string = 'abc'
regex = /./

string.scan(regex) {|x| puts x}

Output is:

a
b
c
infused
  • 24,000
  • 13
  • 68
  • 78
  • No thanks needed. Accept the answer if it answers your question. – infused Aug 20 '14 at 23:37
  • Looking at your question history, you've asked 14 questions which all have at least one answer and you have never accepted any of them. You can accept an answer by clicking the check-mark next to it. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – infused Aug 20 '14 at 23:42
  • How about just: `puts string.scan regex` – pguardiario Aug 21 '14 at 04:25