From: Holden Glova Date: 2002-10-26T13:06:14+09:00 Subject: Re: How to return multiple matches in Regexp? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sat, 26 Oct 2002 15:46, Carl Youngblood wrote: > I'm used to the perl modifier /.../g that makes sure that a regexp > matches as many occurrences as possible. How do you do this in Ruby? > > Thanks, > Carl Youngblood Hi Carl, [synack@Evergreen] synack $ ri String.gsub - ------------------------------------------------------------ String#gsub str.gsub( pattern, replacement ) -> aString str.gsub( pattern ) {| match | block } -> aString - ------------------------------------------------------------------------ Returns a copy of str with all occurrences of pattern replaced with either replacement or the value of the block. If a string is used as the replacement, special variables from the match (such as $& and $1) cannot be substituted into it, as substitution into the string occurs before the pattern match starts. However, the sequences \1, \2, and so on may be used to interpolate successive groups in the match. These sequences are shown in Table 22.7 on page 376. In the block form, the current match is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call. The result inherits any tainting in the original string or any supplied replacement string. "hello".gsub(/[aeiou]/, '*') #=> "h*ll*" "hello".gsub(/([aeiou])/, '<\1>') #=> "hll" "hello".gsub('.') {|s| s[0].to_s + ' '} #=> "104 101 108 108 111 " - -- Signed, Holden Glova -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9uhOY0X8w8X71zPcRAmIeAJ4p8+SgINRf4j2nOrqip9eSBZ7MigCZAdia mmaSmkq93IArzKh+0h/eRNA= =jKeM -----END PGP SIGNATURE-----