Generically, how do you impliment the various flags?
I figured out: s/ / /g --> gsub
and s/ / / -> sub
What about something like
/ /ixsmg
or various combinations thereof?
Generically, how do you impliment the various flags?
I figured out: s/ / /g --> gsub
and s/ / / -> sub
What about something like
/ /ixsmg
or various combinations thereof?
On 4/11/06, Tom A. [email protected] wrote:
Generically, how do you impliment the various flags?
I figured out: s/ / /g → gsub
and s/ / / → sub
Ruby doesn’t support as many regex flags as Perl does.
This is good.
However, %r{…}i will do case insensitive; %r{…}x will do extended.
There is a %r{…}m flag, but it doesn’t quite do the same as some
people expect, I think (it just changes the meaning of . to be able to
match \n, etc. I think). There is no %r{…}g flag, IIRC.
If you simply want a match, you use “string” =~ /regex/options; if you
want to scan something, you might use #grep.
-austin
On Apr 11, 2006, at 1:50 PM, Tom A. wrote:
Generically, how do you impliment the various flags?
I figured out: s/ / /g --> gsub
and s/ / / -> subWhat about something like
/ /ixsmg
or various combinations thereof?
i and x are both there, in the same way, as well as m
e.g.
/ /i
/ /x
/ /ix
/ /m
The only reason /g doesn’t really work is that ruby doesn’t have a
s/// operator (I guess it’s an operator) and that modifier applies
more to the substitution than the regexp itself.
Tom A. wrote:
Generically, how do you impliment the various flags?
I figured out: s/ / /g --> gsub
and s/ / / -> subWhat about something like
/ /ixsmg
or various combinations thereof?
You can apply modifiers (flags) the same as in Perl: /ixm
i => insensitive
x => extended form
m => dot matches newline (same as Perl’s /s flag)
Ruby’s default is Perl’s /m (ie; ^ and $ match at embedded
lines), if you want to match beginning/end of strings see: \A,
\Z, \z (same meanings as in Perl’s regexen).
You may also specify flags within regexen (as in Perl):
(?imx-imx) # flags on/off
(?imx-imx:subpattern) # flags on/off for subpattern
Finally, you may specify flags by ORing their respective
constants when using Regexp::new (IGNORECASE, EXTENDED, MULTILINE
constants in Regexp)
Regexp.new(‘foo’, Regexp::IGNORECASE|Regexp::MULTILINE) # => /foo/mi
As you’ve noticed, Perl’s /g (global) flag is just a different
method call in Ruby (#sub == once, #gsub == globally).
Last, but not least, Perl’s /e (eval flag) can be achieved using
the block form of #sub or #gsub.
regards,
andrew
On 4/11/2006, “Logan C.” [email protected] wrote:
/ /ixsmg
The only reason /g doesn’t really work is that ruby doesn’t have a
s/// operator (I guess it’s an operator) and that modifier applies
more to the substitution than the regexp itself.
I frequently use a perl approach of
while ($arg =~ /(string_thing)/igsm) {
}
to pick out chunks of a string as an iteration through the string.
This would require a ‘g’ without the ‘s’.
No chance?
On Apr 11, 2006, at 2:23 PM, Tom A. wrote:
and s/ / / -> sub
/ /x
while ($arg =~ /(string_thing)/igsm) {use $1 here
}
to pick out chunks of a string as an iteration through the string.
This would require a ‘g’ without the ‘s’.No chance?
I’m pretty sure you want scan
% irb
irb(main):001:0> “bcd”.scan(/[a-z]/i) do |m|
irb(main):002:1* puts m
irb(main):003:1> end
b
c
d
=> “bcd”
Tom A. wrote:
I frequently use a perl approach of
while ($arg =~ /(string_thing)/igsm) {
use $1 here
}
to pick out chunks of a string as an iteration through the string.
This would require a ‘g’ without the ‘s’.No chance?
Use String#scan:
arg.scan(/pattern/im) do |it|
# work on it here
end
andrew
On 4/11/2006, “Andrew J.” [email protected] wrote:
Use String#scan:
arg.scan(/pattern/im) do |it|
work on it here
end
Works, it’s a lot of stuff to remember… again…
Thanks!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs