Forum: Ruby regex flags

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tom A. (Guest)
on 2006-04-11 21:53
(Received via mailing list)
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?
Austin Z. (Guest)
on 2006-04-11 22:08
(Received via mailing list)
On 4/11/06, Tom A. <removed_email_address@domain.invalid> 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
Logan C. (Guest)
on 2006-04-11 22:11
(Received via mailing list)
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/  /  /   ->  sub
>
> What 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.
Andrew J. (Guest)
on 2006-04-11 22:18
Tom A. wrote:
> 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?

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
Tom A. (Guest)
on 2006-04-11 22:24
(Received via mailing list)
On 4/11/2006, "Logan C." <removed_email_address@domain.invalid> 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) {
   #  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?
Andrew J. (Guest)
on 2006-04-11 22:31
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
Logan C. (Guest)
on 2006-04-11 22:34
(Received via mailing list)
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. (Guest)
on 2006-04-11 22:43
(Received via mailing list)
On 4/11/2006, "Andrew J." <removed_email_address@domain.invalid> 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 topic is locked and can not be replied to.