[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6101] RE: Regex question -- new("") vs. //

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-11-07 04:37:29 UTC
List: ruby-talk #6101
Dear Hal,

> Why are these three patterns below not the same?

It's a little bit hard to tell exactly what's going on, as I'm not sure
where you target. 

The thing here is that you have to be careful what you pass around. So 

  Regexp.new(/doesn't need escaping backslashes for digit \d/) 

but 

  Regexp.new("does need, and \d translates to plain d") 

before passing a string to constructor so the backslash flattens out. That
means you have to say Regexp.new("\\d") to say same as Regexp.new(/\d/) or
shorter /\d/.

There are other quirks around too, so beware, and test throughtly before
assuming the Regexp works.

But here's briefly rewritten version of your test program. And after that
the output it generates.

	- Aleksi

  regexps =  [ 
    Regexp.new("^(\d[\d_]*)*\.[\d_]*"),
    /^(\d[\d_]*)*\.[\d_]*/,
    Regexp.new("^(\\d[\\d_]*)*\.[\\d_]*"),

    # these three are equal, note I changed one '*' to '?'
    Regexp.new("^(\\d[\\d_]*)?\\.[\\d_]*"),
    Regexp.new(/^(\d[\d_]*)?\.[\d_]*/),
    /^(\d[\d_]*)?\.[\d_]*/,
  ]

  tests = [ "_123.45", "1_234.56"]

  tests.each do |test|
    puts "Testing: '#{test}'"
    regexps.each_with_index do |re, i|
      print "#{i+1}:  #{re.inspect},  "
      if re.match( test )
        print "\tyes ('#{$&}')"
      else
        print "\tno"
      end
      puts
    end
    puts
  end

And the output:

Testing: '_123.45'
1:  /^(d[d_]*)*.[d_]*/,         yes ('_')
2:  /^(\d[\d_]*)*\.[\d_]*/,     no
3:  /^(\d[\d_]*)*.[\d_]*/,      yes ('_123')
4:  /^(\d[\d_]*)?\.[\d_]*/,     no
5:  /^(\d[\d_]*)?\.[\d_]*/,     no
6:  /^(\d[\d_]*)?\.[\d_]*/,     no

Testing: '1_234.56'
1:  /^(d[d_]*)*.[d_]*/,         yes ('1_')
2:  /^(\d[\d_]*)*\.[\d_]*/,     yes ('1_234.56')
3:  /^(\d[\d_]*)*.[\d_]*/,      yes ('1_234.56')
4:  /^(\d[\d_]*)?\.[\d_]*/,     yes ('1_234.56')
5:  /^(\d[\d_]*)?\.[\d_]*/,     yes ('1_234.56')
6:  /^(\d[\d_]*)?\.[\d_]*/,     yes ('1_234.56')

In This Thread

Prev Next