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
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
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>') #=> "h<e>ll<o>"
"hello".gsub('.') {|s| s[0].to_s + ' '} #=> "104 101 108 108 111 "
Signed,
Holden Glova
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 –
On Sat, 26 Oct 2002, 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?
See Holden’s reference to gsub; but also, if you don’t need to sub
anything, there’s String#scan:
ruby -e ‘p “one two three”.scan(/\w+/)’
[“one”, “two”, “three”]
David
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
aString.scan(aRegexp) → anArray
… may be what you’re looking for.
refer to: http://www.rubycentral.com/book/ref_c_string.html#String.scan
On Friday 25 October 2002 10:46 pm, 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