Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ignore spaces in string match

Reply
Thread Tools

Ignore spaces in string match

 
 
Thelma Lubkin
Guest
Posts: n/a
 
      11-20-2004
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
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      11-20-2004

"bengee" <(E-Mail Removed)> wrote in message
news:419f5c91$0$33604$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-20-2004
Thelma Lubkin <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Thelma Lubkin
Guest
Posts: n/a
 
      11-20-2004
Matt Garrish <(E-Mail Removed)> 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


 
Reply With Quote
 
Thelma Lubkin
Guest
Posts: n/a
 
      11-20-2004
Thelma Lubkin <(E-Mail Removed)> wrote:
: Matt Garrish <(E-Mail Removed)> 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


 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      11-20-2004

"Thelma Lubkin" <(E-Mail Removed)> wrote in message
news:cnntap$37g$(E-Mail Removed)...
> Matt Garrish <(E-Mail Removed)> 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


 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-22-2004
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 <(E-Mail Removed)>
"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)
 
Reply With Quote
 
Thelma Lubkin
Guest
Posts: n/a
 
      11-22-2004
Tad McClellan <(E-Mail Removed)> 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
: (E-Mail Removed) Perl programming
: Fort Worth, Texas
 
Reply With Quote
 
Janek Schleicher
Guest
Posts: n/a
 
      11-22-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ignore + TEST + Ignore SpooderStank Computer Support 2 04-08-2004 11:26 AM
Searching for Exact Phrase - should I ignore the ignore words? Rob Meade ASP General 6 03-01-2004 11:28 AM



Advertisments