Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Help with my regex
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Jason Kinkade
09-30-04 07:56 AM


Re: Help with my regex
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".

/te(\S*)st/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
09-30-04 07:56 AM


Re: Help with my regex
Gunnar 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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
09-30-04 07:56 AM


Re: Help with my regex
On 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



Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'japhy' Pinyan
09-30-04 07:56 AM


Re: Help with my regex
Gunnar 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)

Report this thread to moderator Post Follow-up to this message
Old Post
Shawn Corey
09-30-04 04:35 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.