Looking for a regular pattern

You can, but alternation is more work for the regex engine:

#!/usr/bin/env ruby -wKU

require "benchmark"

DATA = ("a".."z").to_a.join
TESTS = 100_000

Benchmark.bmbm do |results|
   results.report("/./m:") { TESTS.times { DATA.scan(/./m) } }
   results.report("/(?m:.)/:") { TESTS.times { DATA.scan(/(?m:.)/) } }
   results.report("/(.|\\n)/:") { TESTS.times { DATA.scan(/(.|\n)/) } }
end
# >> Rehearsal ---------------------------------------------
# >> /./m: 3.490000 0.100000 3.590000 ( 3.617568)
# >> /(?m:.)/: 3.560000 0.100000 3.660000 ( 3.687604)
# >> /(.|\n)/: 5.010000 0.120000 5.130000 ( 5.190762)
# >> ----------------------------------- total: 12.380000sec
# >>
# >> user system total real
# >> /./m: 3.510000 0.110000 3.620000 ( 3.636716)
# >> /(?m:.)/: 3.560000 0.100000 3.660000 ( 3.671196)
# >> /(.|\n)/: 5.030000 0.120000 5.150000 ( 5.154031)

__END__

James Edward Gray II

···

On Sep 1, 2008, at 5:11 AM, Patrick He wrote:

Additionally, you can use pattern "(.|\n)" if you want to match all
characters including "\n" in single line mode.

In case you didn't see James Gray's correction, let me inform you what
I've written here is rubbish. The dot in a character class is just a
dot, not a wildcard.

David

···

On Mon, 1 Sep 2008, David A. Black wrote:

Hi --

On Mon, 1 Sep 2008, Peter Hickman wrote:

Zhao Yi wrote:

Sebastian Hungerecker wrote:

Zhao Yi wrote:

I think this is better [.\s]

No, it's not.

Why? It should match any character.

From the docs, http://www.ruby-doc.org/docs/UsersGuide/rg/regexp.html

>\s| space character; same as |[ \t\n\r\f]

That is not any character it is only whitespace.

Yes, but [.\s] is a character class, consisting of any character other
than \n, plus any space including \n. So it will match any character.

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

Hi --

···

On Tue, 2 Sep 2008, James Gray wrote:

On Sep 1, 2008, at 4:27 AM, David A. Black wrote:

Hi --

On Mon, 1 Sep 2008, Peter Hickman wrote:

Zhao Yi wrote:

Sebastian Hungerecker wrote:

Zhao Yi wrote:

I think this is better [.\s]

No, it's not.

Why? It should match any character.

From the docs, http://www.ruby-doc.org/docs/UsersGuide/rg/regexp.html

>\s| space character; same as |[ \t\n\r\f]

That is not any character it is only whitespace.

Yes, but [.\s] is a character class, consisting of any character other
than \n, plus any space including \n. So it will match any character.

No, it's not:

"a" =~ /[.\n]/

=> nil

The period loses its special meaning inside a character class, so that class literally matches a period or a newline character.

Whoops. Yes, absolutely -- oldest pitfall in the book and I stepped
right into it :slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!