Ignore spaces in string match

Discussion in 'Perl Misc' started by Thelma Lubkin, Nov 20, 2004.

  1. I am trying to compare two strings that must match except for their
    spacing.

    If I write:

    $h1 = 'here i am';
    $h2 = 'herei am';

    can I do a less clumsy comparison than

    ($hh2 = $h2) =~ s/\s//x;
    if($hh2 =~ /^$h1$/x) ...

    --thanks, thelma
    Thelma Lubkin, Nov 20, 2004
    #1
    1. Advertising

  2. Thelma Lubkin

    Matt Garrish Guest

    "bengee" <> wrote in message
    news:419f5c91$0$33604$...
    > Thelma Lubkin wrote:
    >> I am trying to compare two strings that must match except for their
    >> spacing.

    >
    > Remove spaces from both strings... if they match then they match!
    >


    If you looked at the example, you'd have noticed that the OP already figured
    that part out. To the OP, elegance is a matter of taste, but if you're just
    looking for speed you could use tr instead of the regexes:

    (my $copy1 = $h1) =~ tr/ //d;
    (my $copy2 = $h2) =~ tr/ //d;

    if ($copy1 eq $copy2) {

    }

    Without modifying the original variables, I don't expect you find anything
    nicer.

    As a side note, the code you posted doesn't work. For one, you're only
    removing a single space from one of the strings.You're also using the /x
    modifier in the first instance where you should be using /g and in the
    second where it makes no sense at all. Please see the perlre doc page for
    more information on regex modifiers and what they do.

    Matt
    Matt Garrish, Nov 20, 2004
    #2
    1. Advertising

  3. Thelma Lubkin <> wrote:
    > I am trying to compare two strings that must match except for their
    > spacing.
    >
    > If I write:
    >
    > $h1 = 'here i am';
    > $h2 = 'herei am';
    >
    > can I do a less clumsy comparison than
    >
    > ($hh2 = $h2) =~ s/\s//x;



    ( the s///x option is a no-op there, you must have meant s///g instead?)

    ( that ignores characters other than the space character. Is that
    what you intended? if not then: s/ //g, or better: tr/ //d.
    )

    I don't think so, normalizing the data before the comparison seems
    the best approach to me. You would of course need a similarly
    de-spaced copy of $h1 too though.


    > if($hh2 =~ /^$h1$/x) ...



    Do you intend for $h1 to contain regex metacharacters that *are* meta?

    If not, then what you have there is an equality test disguised as a
    pattern match and implemented in an "expensive" way.

    if ( $hh2 eq $hh1 ) # more clear AND faster!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 20, 2004
    #3
  4. Matt Garrish <> wrote:
    : If you looked at the example, you'd have noticed that the OP already figured
    : that part out. To the OP, elegance is a matter of taste, but if you're just
    : looking for speed you could use tr instead of the regexes:

    : (my $copy1 = $h1) =~ tr/ //d;
    : (my $copy2 = $h2) =~ tr/ //d;
    : if ($copy1 eq $copy2) {
    : }
    Thanks for the tip: I am going to need many repeats.

    : As a side note, the code you posted doesn't work. For one, you're only
    : removing a single space from one of the strings.You're also using the /x
    : modifier in the first instance where you should be using /g and in the
    : second where it makes no sense at all. Please see the perlre doc page for
    : more information on regex modifiers and what they do.

    That first instance was certainly a careless glitch on my
    part.

    But if the second /x makes no sense, why does this code work
    as I want it to? I've read that /x is recommended for making
    complex regular expressions more readable, but it seems to
    work also for this use ??

    --thelma, trying to learn, not argue


    #!/usr/bin/perl -w
    #use strict;


    my $h1 = 'here i am';
    my $h2 = 'herei am';
    ($hh2 = $h2) =~ s/\s//g;
    if($hh2 =~ /^$h1$/x)
    { print "I'm ok for: $h1\n"; }
    else
    { print "I'm in a mess for $h1\n"; }

    $h3 = 'where am I?';
    if($hh2 =~ /^$h3$/x)
    { print "I'm ok for: $h3\n"; }
    else
    { print "I'm in a mess for: $h3\n"; }


    : Matt
    Thelma Lubkin, Nov 20, 2004
    #4
  5. Thelma Lubkin <> wrote:
    : Matt Garrish <> wrote:
    : : As a side note, the code you posted doesn't work. For one, you're only
    : : removing a single space from one of the strings.You're also using the /x
    : : modifier in the first instance where you should be using /g and in the
    : : second where it makes no sense at all. Please see the perlre doc page for
    : : more information on regex modifiers and what they do.

    : That first instance was certainly a careless glitch on my
    : part.

    : But if the second /x makes no sense, why does this code work
    : as I want it to? I've read that /x is recommended for making
    : complex regular expressions more readable, but it seems to
    : work also for this use ??

    : --thelma, trying to learn, not argue



    ########## Sorry: I can't seem to do anything right this morning.
    : #!/usr/bin/perl -w
    : #use strict;
    ^ remove

    : my $h1 = 'here i am';
    : my $h2 = 'herei am';
    my $hh2;
    ^ add
    : ($hh2 = $h2) =~ s/\s//g;
    : if($hh2 =~ /^$h1$/x)
    : { print "I'm ok for: $h1\n"; }
    : else
    : { print "I'm in a mess for $h1\n"; }

    : my $h3 = 'where am I?';
    : ^ add

    if($hh2 =~ /^$h3$/x)
    : { print "I'm ok for: $h3\n"; }
    : else
    : { print "I'm in a mess for: $h3\n"; }


    : : Matt
    Thelma Lubkin, Nov 20, 2004
    #5
  6. Thelma Lubkin

    Matt Garrish Guest

    "Thelma Lubkin" <> wrote in message
    news:cnntap$37g$...
    > Matt Garrish <> wrote:
    >
    > : As a side note, the code you posted doesn't work. For one, you're only
    > : removing a single space from one of the strings.You're also using the /x
    > : modifier in the first instance where you should be using /g and in the
    > : second where it makes no sense at all. Please see the perlre doc page
    > for
    > : more information on regex modifiers and what they do.
    >
    > That first instance was certainly a careless glitch on my
    > part.
    >
    > But if the second /x makes no sense, why does this code work
    > as I want it to? I've read that /x is recommended for making
    > complex regular expressions more readable, but it seems to
    > work also for this use ??
    >


    Because the /x modifier is for adding comments to your regex. You have no
    comments or whitespace in your regex, so it's pointless to use the modifier.
    Using a modifier for no apparent reason tends to indicate that you either
    don't fully understand what they do or why they're there. I was just trying
    to point out to you that you should review them. Modifiers should only be
    used when you need them, otherwise you introduce the possibility of unwanted
    bugs when they start "doing" what they're intended to do.

    Matt
    Matt Garrish, Nov 20, 2004
    #6
  7. Thelma Lubkin

    Tore Aursand Guest

    On Sat, 20 Nov 2004 14:50:39 +0000, Thelma Lubkin wrote:
    > I am trying to compare two strings that must match except for their
    > spacing.
    >
    > If I write:
    >
    > $h1 = 'here i am';
    > $h2 = 'herei am';
    >
    > can I do a less clumsy comparison than
    >
    > ($hh2 = $h2) =~ s/\s//x;
    > if($hh2 =~ /^$h1$/x) ...


    Remove the spaces in both strings, then do the match;

    sub compare_strings {
    my $s1 = shift;
    my $s2 = shift;

    $s1 =~ tr/ //d;
    $s2 =~ tr/ //d;

    return ( $s1 eq $s2 );
    }


    --
    Tore Aursand <>
    "I call upon all nations to do everything they can to stop these
    terrorist killers. Thank you. Now watch this drive." (George W. Bush,
    while playing golf)
    Tore Aursand, Nov 22, 2004
    #7
  8. Tad McClellan <> wrote:
    :> if($hh2 =~ /^$h1$/x) ...

    : Do you intend for $h1 to contain regex metacharacters that *are* meta?

    : If not, then what you have there is an equality test disguised as a
    : pattern match and implemented in an "expensive" way.

    It was the preceding sentence that really showed me
    how hopeless and useless--and silly--my approach was.
    I will do the tr and then the eq comparision.

    Thanks to everyone who helped me with this.
    --thelma

    : if ( $hh2 eq $hh1 ) # more clear AND faster!


    : --
    : Tad McClellan SGML consulting
    : Perl programming
    : Fort Worth, Texas
    Thelma Lubkin, Nov 22, 2004
    #8
  9. On Sat, 20 Nov 2004 14:50:39 +0000, Thelma Lubkin wrote:

    > I am trying to compare two strings that must match except for their
    > spacing.
    >
    > If I write:
    >
    > $h1 = 'here i am';
    > $h2 = 'herei am';
    >
    > can I do a less clumsy comparison than
    >
    > ($hh2 = $h2) =~ s/\s//x;
    > if($hh2 =~ /^$h1$/x) ...


    You could also use the CPAN module String::Compare, e.g.:

    use String::Compare;

    my $h1 = 'here i am';
    my $h2 = 'herei am';

    if (String::Compare::chars_only($h1,$h2) eq 1) {
    print "...";
    }


    Greetings,
    Janek
    Janek Schleicher, Nov 22, 2004
    #9
    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. John B. Matthews
    Replies:
    4
    Views:
    667
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    623
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,597
    Jan C. Faerber
    Sep 10, 2009
  4. Rob Meade
    Replies:
    6
    Views:
    264
    Rob Meade
    Mar 1, 2004
  5. asm_toronto

    Need help to ignore leading spaces

    asm_toronto, Apr 4, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    122
    John W. Krahn
    Apr 5, 2006
Loading...

Share This Page