Code Comments
Programming Forum and web based access to our favorite programming groups.I want to match any non-whitespace character between the letters of a word. "te.?st" will obviously match any single character between "te" and "st" including a space and nothing. I want that, but excluding the space. I tried everything, like /te(.?[^\s])st/ doesn't do it, cause I still want "nothing". Thanks.
Post Follow-up to this messageJason Kinkade wrote: > I want to match any non-whitespace character between the letters of a > word. > > "te.?st" will obviously match any single character between "te" and > "st" including a space and nothing. I want that, but excluding the > space. > > I tried everything, like /te(.?[^\s])st/ doesn't do it, cause I still > want "nothing". /te(\S*)st/ -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
Post Follow-up to this messageGunnar Hjalmarsson wrote: > > /te(\S*)st/ Or rather /te(\S*?)st/ I suppose. But it depends of course on what exactly the purpose of the regex is, and I leave it to you to figure out the difference. Have you studied perldoc perlrequick perldoc perlretut perldoc perlre ?? -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
Post Follow-up to this messageOn 29 Sep 2004, Jason Kinkade wrote: >I want to match any non-whitespace character between the letters of a >word. > >"te.?st" will obviously match any single character between "te" and >"st" including a space and nothing. I want that, but excluding the >space. > >I tried everything, like /te(.?[^\s])st/ doesn't do it, cause I still >want "nothing". You mean you want to match "test" or "teXst", where X is a non-whitespace character? /te\S?st/ -- Jeff "japhy" Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service Senior Dean, Fall 2004 % have long ago been overpaid? RPI Corporation Secretary % http://japhy.perlmonk.org/ % -- Meister Eckhart
Post Follow-up to this messageGunnar Hjalmarsson wrote:
> Or rather
>
> /te(\S*?)st/
>
> I suppose. But it depends of course on what exactly the purpose of the
> regex is, and I leave it to you to figure out the difference.
Mileage may vary.
--- Shawn
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @tests = (
"test",
"te st",
"teXst",
"te st",
"teXXXXXXXXXst",
"te\xA0st",
"te\tst",
'te' . ( ' ' x 1_000 ) . 'st',
'te' . ( 'X' x 1_000 ) . 'st',
);
for my $s ( @tests ){
print "Testing $s\n";
timethese( 1_000_000, {
'greedy \S* ' => sub { $s =~ /te(\S*)st/; },
'frugal \S*?' => sub { $s =~ /te(\S*?)st/; },
});
print "\n";
}
__END__
Testing test
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 3 wallclock secs ( 2.92 usr + 0.00 sys = 2.92 CPU) @
342465.75/s (n=1000000)
greedy \S* : 3 wallclock secs ( 3.27 usr + 0.00 sys = 3.27 CPU) @
305810.40/s (n=1000000)
Testing te st
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 2 wallclock secs ( 1.76 usr + 0.00 sys = 1.76 CPU) @
568181.82/s (n=1000000)
greedy \S* : 2 wallclock secs ( 1.75 usr + 0.00 sys = 1.75 CPU) @
571428.57/s (n=1000000)
Testing teXst
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 3 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) @
323624.60/s (n=1000000)
greedy \S* : 3 wallclock secs ( 3.21 usr + 0.00 sys = 3.21 CPU) @
311526.48/s (n=1000000)
Testing te st
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 2 wallclock secs ( 1.89 usr + 0.00 sys = 1.89 CPU) @
529100.53/s (n=1000000)
greedy \S* : 2 wallclock secs ( 1.84 usr + 0.00 sys = 1.84 CPU) @
543478.26/s (n=1000000)
Testing teXXXXXXXXXst
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 4 wallclock secs ( 3.35 usr + 0.00 sys = 3.35 CPU) @
298507.46/s (n=1000000)
greedy \S* : 3 wallclock secs ( 3.39 usr + 0.00 sys = 3.39 CPU) @
294985.25/s (n=1000000)
Testing te�st
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 3 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @
313479.62/s (n=1000000)
greedy \S* : 4 wallclock secs ( 3.21 usr + 0.00 sys = 3.21 CPU) @
311526.48/s (n=1000000)
Testing te st
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 1 wallclock secs ( 1.73 usr + 0.00 sys = 1.73 CPU) @
578034.68/s (n=1000000)
greedy \S* : 2 wallclock secs ( 1.75 usr + 0.00 sys = 1.75 CPU) @
571428.57/s (n=1000000)
Testing te
st
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 19 wallclock secs (18.35 usr + 0.00 sys = 18.35 CPU) @
54495.91/s (n=1000000)
greedy \S* : 5 wallclock secs ( 5.87 usr + 0.00 sys = 5.87 CPU) @
170357.75/s (n=1000000)
Testing
teXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXst
Benchmark: timing 1000000 iterations of frugal \S*?, greedy \S* ...
frugal \S*?: 32 wallclock secs (31.42 usr + 0.00 sys = 31.42 CPU) @
31826.86/s (n=1000000)
greedy \S* : 19 wallclock secs (18.75 usr + 0.00 sys = 18.75 CPU) @
53333.33/s (n=1000000)
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.