Help with my regex

Discussion in 'Perl Misc' started by Jason Kinkade, Sep 30, 2004.

  1. 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.
    Jason Kinkade, Sep 30, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Sep 30, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Sep 30, 2004
    #3
  4. 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
    Jeff 'japhy' Pinyan, Sep 30, 2004
    #4
  5. Jason Kinkade

    Shawn Corey Guest

    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
    teXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    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)
    Shawn Corey, Sep 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    688
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,606
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    589
  4. Xah Lee
    Replies:
    1
    Views:
    929
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    730
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page