Looking for a regular pattern

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.

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 G. II

Hi –

On Tue, 2 Sep 2008, James G. wrote:

No, it’s not.
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

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

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.

In case you didn’t see James G.'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