Regular Expression: Searching and Replacing 2 or more digits in Ruby

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


1 point

I found this regular expression to match 2 or more digits in Ruby and then replace it:

a = %w(4 1 1 0 4 4 0 0 4 4 4)
a.join.gsub(/4{2,}/,'ab')

Output would be

4 1 1 0 a b 0 0 a b



Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.