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.