Regexp imxo - does 'x' work?

should this not work ?

irb(main):001:0> %r{
irb(main):002:0/ foo
irb(main):003:0/ #foo
irb(main):004:0/ bar
irb(main):005:0/ #bar
irb(main):006:0/ }
/\nfoo\n#foo\nbar\n#bar\n/

or this?

irb(main):007:0> %r{foo bar}x
/foo bar/x

what DOES ‘x’ do?

perl -e ‘print “foobar” =~ /foo bar/x’ >> 1
ruby -e ‘print “foobar” =~ /foo bar/x’ >> 0

confused.

-a

···

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

should this not work ?

irb(main):001:0> %r{
irb(main):002:0/ foo
irb(main):003:0/ #foo
irb(main):004:0/ bar
irb(main):005:0/ #bar
irb(main):006:0/ }
/\nfoo\n#foo\nbar\n#bar\n/

That’s working, isn’t it?

or this?

irb(main):007:0> %r{foo bar}x
/foo bar/x

what DOES ‘x’ do?

It’s an eXtended regex, which means that literal spaces and
newlines are ignored.

perl -e ‘print “foobar” =~ /foo bar/x’ >> 1

Yikes, I can’t even remember what Perl returns for these
cases…

ruby -e ‘print “foobar” =~ /foo bar/x’ >> 0

The 0 is the index into the string of where the match
starts – and there was indeed a match.

Here are some more examples:

irb(main):017:0> /foo bar/.match(“foobar”)
nil

irb(main):018:0> /foo bar/x.match(“foobar”)
#MatchData:0x401a3814

Throwing in a newline:

irb(main):019:0> /foo
irb(main):020:0/ b a r/x.match(“foobar”)
#MatchData:0x401a0a88

David

···

On Fri, 6 Dec 2002, ahoward wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

[…]

perl -e ‘print “foobar” =~ /foo bar/x’ >> 1
ruby -e ‘print “foobar” =~ /foo bar/x’ >> 0

Perl is returning whether there was a match (undef if no match).
Ruby is returning the offset of the match (strings start at 0).

See the other (by dblack) for what /x does.

cheers,

···


Iain.

[snip]

should this not work ?

irb(main):001:0> %r{
irb(main):002:0/ foo
irb(main):003:0/ #foo
irb(main):004:0/ bar
irb(main):005:0/ #bar
irb(main):006:0/ }
/\nfoo\n#foo\nbar\n#bar\n/

That’s working, isn’t it?
[snip]

i guess. it looked like the comments were being included in the regex… i
guess they are not.

since ruby’s comments continue till line’s end, i guess there is no way to
embed comments in a single line regex? or is there?

-a

···

On Fri, 6 Dec 2002 dblack@candle.superlink.net wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi –

[snip]

should this not work ?

irb(main):001:0> %r{
irb(main):002:0/ foo
irb(main):003:0/ #foo
irb(main):004:0/ bar
irb(main):005:0/ #bar
irb(main):006:0/ }
/\nfoo\n#foo\nbar\n#bar\n/

That’s working, isn’t it?
[snip]

i guess. it looked like the comments were being included in the regex… i
guess they are not.

Oh, I see what you mean – but yes, they are included. Hmmm… I was
going to say, use a literal instead of %r{} to get the comments in,
but that seems not to be available:

ruby -e ‘p /abc
#blah
/x’
/abc\n#blah\n/x

since ruby’s comments continue till line’s end, i guess there is no
way to embed comments in a single line regex? or is there?

All I can think of is:

irb(main):003:0> /ab#{“This is a comment”*0}c/.match(“abc”)
#MatchData:0x401e3990

:slight_smile:

David

···

On Fri, 6 Dec 2002, ahoward wrote:

On Fri, 6 Dec 2002 dblack@candle.superlink.net wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

[…]

since ruby’s comments continue till line’s end, i guess there is no way to
embed comments in a single line regex? or is there?

puts “Foo” =~ /Fo(?# this is a comment )o/

Better of using /x imho. Makes things more readable.

cheers,

···


Iain.

Hi,

···

At Fri, 6 Dec 2002 11:59:12 +0900, dblack@candle.superlink.net wrote:

since ruby’s comments continue till line’s end, i guess there is no
way to embed comments in a single line regex? or is there?

All I can think of is:

irb(main):003:0> /ab#{“This is a comment”*0}c/.match(“abc”)
#MatchData:0x401e3990

In 1.7, this is more efficient.
/ab#{“This is a comment”;“”}c/.match(“abc”)

Preceding literal is eliminated by successive literal, the
regexp is compiled to a static literal.


Nobu Nakada

nobu.nokada@softhome.net wrote in message

In 1.7, this is more efficient.
/ab#{“This is a comment”;“”}c/.match(“abc”)

Preceding literal is eliminated by successive literal, the
regexp is compiled to a static literal.

Sorry, nobu … I did not get the hang of this.
Even after doing:

C:\ruby\bin>ruby -v
ruby 1.7.3 (2002-11-17) [i386-mswin32]

C:\ruby\bin>ruby -e 'puts “#{“Hello”}” ’
Hello

C:\ruby\bin>ruby -e 'puts “#{“Hello”;”“}” ’

C:\ruby\bin>

Why does the preceding literal get eliminated by ;“” ?
Is this some thing new in 1.7?
Please help. Thanks,
– shanko

Hi,

Why does the preceding literal get eliminated by ;“” ?

“#{“Hello”;”“}” is equivalent to (“Hello”;“”).to_s or
begin
“Hello”
“”
end.to_s

Their value is one of the last expression, so value of the
preceding literal is always ignored and has no effect (except
for GC).

Is this some thing new in 1.7?

It’s warned even in 1.6, but elimination is new in 1.7.

$ ruby-1.6 -v -e ‘p “#{“Hello”;”“}”’
ruby 1.6.8 (2002-12-02) [i686-linux]
-e:1: warning: useless use of a literal in void context
“”

···

At Fri, 6 Dec 2002 13:58:14 +0900, Shashank Date wrote:


Nobu Nakada

Great !

Thanks,

···

nobu.nokada@softhome.net wrote

“#{“Hello”;”“}” is equivalent to (“Hello”;“”).to_s or
begin
“Hello”
“”
end.to_s