You may be better off doing the initial check without using the regex engine; only using it with split where necessary.

As you can see from ++aaron_baugher's analysis, your results will depend on your real data. Furthermore, if your volume of data is small, your choice of solution may make little difference (in terms of runtime).

Here's a solution using substr, rindex and length for the initial check. As a proof-of-concept to show that these functions work on characters (as opposed to bytes), I've included single-byte and multi-byte characters in the data.

#!/usr/bin/env perl -l use strict; use warnings; use utf8; use open OUT => ':utf8', ':std'; my @strings = ( 'A B C', 'D E', 'F ', 'G', 'H ', 'I ', "\N{MERCURY} \N{FEMALE SIGN} \N{EARTH}", "\N{MALE SIGN} \N{JUPITER +}", "\N{SATURN} ", "\N{URANUS}", "\N{NEPTUNE} ", "\N{PLUTO} ", ); for (@strings) { next if substr($_, -1, 1) eq ' ' or rindex($_, ' ', length() - 2) +== -1; print; }

Output:

A B C
D E
☿ ♀ ♁
♂ ♃

[The Unicode range of characters labelled "Astrological symbols" is 0x263d to 0x2647. There is no charname for "VENUS" or "MARS"; the charnames "FEMALE SIGN" and "MALE SIGN" are defined for these symbols, respectively.]

Here's a benchmark test. This uses my sample data. If you choose this route, you should benchmark with representative samples of your data.

#!/usr/bin/env perl -l use strict; use warnings; use Benchmark qw{cmpthese}; my @strings = ( 'A B C', 'D E', 'F ', 'G', 'H ', 'I ', "\N{MERCURY} \N{FEMALE SIGN} \N{EARTH}", "\N{MALE SIGN} \N{JUPITER +}", "\N{SATURN} ", "\N{URANUS}", "\N{NEPTUNE} ", "\N{PLUTO} ", ); my $re = qr{\s+\b}; cmpthese -1 => { no_re_check_and_split => \&no_re_check_and_split, re_check_and_split => \&re_check_and_split, split_and_re_check => \&split_and_re_check, }; sub no_re_check_and_split { for (@strings) { next if substr($_, -1, 1) eq ' ' or rindex($_, ' ', length() - + 2) == -1; my @parts = split /$re/; } } sub re_check_and_split { for (@strings) { next unless /$re/; my @parts = split /$re/; } } sub split_and_re_check { for (@strings) { my @parts = split /$re/; next if @parts > 1; } }

Here's three sample runs:

Rate split_and_re_check re_check_and_split no +_re_check_and_split split_and_re_check 50243/s -- -18% + -29% re_check_and_split 61265/s 22% -- + -13% no_re_check_and_split 70274/s 40% 15% + -- Rate split_and_re_check re_check_and_split no +_re_check_and_split split_and_re_check 53593/s -- -19% + -27% re_check_and_split 66370/s 24% -- + -10% no_re_check_and_split 73770/s 38% 11% + -- Rate split_and_re_check re_check_and_split no +_re_check_and_split split_and_re_check 53096/s -- -20% + -27% re_check_and_split 66369/s 25% -- + -8% no_re_check_and_split 72404/s 36% 9% + -- ken@ganymede: ~/tmp

With my sample data, doing the initial check without a regex appears faster. Again, I'll stress, you'll need to check with your data.

-- Ken


In reply to Re: Check for Spaces in a String by kcott
in thread Check for Spaces in a String by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.