Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby doesn't recognize the g flag for regex

Tags:

regex

ruby

Is it implied by default in str.scan? Is it off by default in str[regex] ?

like image 941
Zombies Avatar asked Dec 29 '22 01:12

Zombies


2 Answers

Yes, how often the regex is applied depends on the method used, not on the regex's flags.

scan will return an array containing (or iterate over) all matches of the regex. match and String#[] will return the first match. =~ will return the index of the first match. gsub will replace all occurrences of the regex and sub will replace the first occurence.

like image 100
sepp2k Avatar answered Jan 05 '23 06:01

sepp2k


smotchkkiss:~$ irb
>> 'Foobar does not like food because he is a fool'.gsub(/foo/i, 'zim')
=> "zimbar does not like zimd because he is a ziml"
like image 27
maček Avatar answered Jan 05 '23 06:01

maček