Exact string matches 
Author Message
 Exact string matches


Quote:

>Hi

>I am looking to match the end of a sting exactly ie I want:

>if($string =~ $anotherstring){
>     do something;
>}

>This works but matches any bit of $anotherstring to $string
>eg

>$string = ".baker"; $ anothersting = ".ba";

>and gives a match.  How do I code the test so that the only matches I
>get are for other strings that end in .ba alone?

One thing you could do is say something like

  if ($string =~ /\Q$anotherstring\E$/) {
    ...
  }

which uses the $ character to anchor the match to the end of $string and
used \Q and \E to bracket escaping any metacharacters in $anotherstring -
the . in $anotherstring will match any character (usually except \n, see
the perlre man page or pod documentation to investigate the detail of
perl's regexes)

Alternatively if you're matching exact sequences of characters you might
use rindex and length or substr and length and the eq string equality
operator - this is perl and there's more than one way to do it, but I
think the regex produces simpler code...

Hope this helps,

Mike

--

http://www.*-*-*.com/ ~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.*-*-*.com/ ;  |                   65 F3 3F 1D 27 22 B7 41



Mon, 24 Apr 2000 03:00:00 GMT  
 Exact string matches

Quote:

>I am looking to match the end of a sting exactly ie I want:
>if($string =~ $anotherstring){
>     do something;
>}

What you are looking for is probably the "eq" operator

if($string eq $anotherstring) {}

You can also do almost the same thing by anchoring and quoting a
regular expression with the match operator, but less efficiently:

if($string =~ /^\Q$anotherstring$/) {}

Notice that the right hand side of the "=~" operator is a regular
expression match operator. According to the documentation, the right
hand side of the =~ operator should be a match operator (m//, but
maybe without the m), a substitution operator (the s/// operator), or
a transliteration operator (tr/// or y/// operators).

--
Andrew Langmead



Mon, 24 Apr 2000 03:00:00 GMT  
 Exact string matches

Quote:

> I am looking to match the end of a sting exactly ie I want:
>    if($string =~ $anotherstring){
>       do something;
>    }
> This works but matches any bit of $anotherstring to $string e.g.
>    $string = ".baker"; $ anothersting = ".ba";
> and gives a match.  How do I code the test so that the only matches I
> get are for other strings that end in .ba alone?

If you're trying to check suffixes for filenames or something:

    my $string = "funky.ba";
    my $suffix = substr($string, rindex($string, '.'));

    if ($suffix eq '.ba') {
       # do something
    }

To do it with a regexp you would add an "anchor". ($ = end-of-string)
The \Q and \E quote $anotherstring just in case it has ., *, +, etc. in it.

    if ($string =~ /\Q$anotherstring\E$/) {
       # do something
    }

in general you'll find that using regular expressions will sometimes be
cleaner, but substr() etc. will execute faster.

hope this helps,
mark

--                                                  _________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Northern Telecom Ltd. |
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   | Box 3511, Station 'C' |
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, ON    K1Y 4H7 |



Mon, 24 Apr 2000 03:00:00 GMT  
 Exact string matches

Hi

I am looking to match the end of a sting exactly ie I want:

if($string =~ $anotherstring){
     do something;

Quote:
}

This works but matches any bit of $anotherstring to $string
eg

$string = ".baker"; $ anothersting = ".ba";

and gives a match.  How do I code the test so that the only matches I
get are for other strings that end in .ba alone?

Please email any suggestions to


TIA

Paul



Mon, 24 Apr 2000 03:00:00 GMT  
 Exact string matches



Quote:
> Hi

> I am looking to match the end of a sting exactly ie I want:

> if($string =~ $anotherstring){
>      do something;
> }

> This works but matches any bit of $anotherstring to $string
> eg

> $string = ".baker"; $ anothersting = ".ba";

> and gives a match.  How do I code the test so that the only matches I
> get are for other strings that end in .ba alone?

Well, I dont really understand the question... but you can do...

if ($a eq $b) { }   - To compare two strings exactly....
if ($a =~ /^$b$/) { } - To compare two strings exactly but use regexps
if ($a =~ /$b$/) { } - To match strings that end in $b

Hope that answers your question....

James



Tue, 25 Apr 2000 03:00:00 GMT  
 Exact string matches

I need help too.
I have a script which search a file for text entered on formpage.

I have something like this:

#!/usr/bin/perl
...
...
$file = 'data.html';

&ReadParse;
$TEXT = $in{'text'};

print "Content-Type: text/html\n\n";
print $head;

open(PLIK,"$file") || die "Open $file: $!\n";
while (<PLIK>) {
        if (/^<h3>([^<]+)<\/h3>/) { # find <h3> case insensitive
                $found = 1 if $1 eq $TEXT; # or => $1 =~ /^$TEXT$/i
        }
        last if $found && /^<hr>/i; # the end of the data record
        print if $found;

Quote:
}

close(PLIK) || die "Close $file: $!\n";

print "$TEXT not found. ;-(<br>\n" if !($found);

But when I have an entry like this:

<H3>text-1</H3>
...
...
...
<HR>
<H3>text-2</H3>
...
...
...
<HR>
<H3>text-2</H3>
...
...
...
<HR>
<H3>text-1</H3>
...
...
...
<HR>

and would like to find and display all lines matches text-1
I recive also text-1 and following lines and next text-1 and
finaly last entry with text-1.
Can you tell me how to display all entries matching text-1 if
text-1 entry and the following lines are scatterd diffrent places
inside a data.html file.

Jerzy

Quote:
> Well, I dont really understand the question... but you can do...

> if ($a eq $b) { }   - To compare two strings exactly....
> if ($a =~ /^$b$/) { } - To compare two strings exactly but use regexps
> if ($a =~ /$b$/) { } - To match strings that end in $b

> Hope that answers your question....

> James



Tue, 25 Apr 2000 03:00:00 GMT  
 Exact string matches


Quote:
> if ($a eq $b) { }   - To compare two strings exactly....
> if ($a =~ /^$b$/) { } - To compare two strings exactly but use regexps
> if ($a =~ /$b$/) { } - To match strings that end in $b

Actually, those last two descriptions are misleading. Those two work at
all only if $b is a valid portion of a regex which may be interpolated
there. If $b contains, for example, ')', that won't do anything good.

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Tue, 25 Apr 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. string comparison - exact matching...

2. Regexp (exact match wanted)

3. Exact match

4. GD module question: not matching exact colors (DigitalColor Monitor)

5. question about exact match

6. Exact match with the Regular expression

7. match exact n times?

8. Exact match, but Insensitive

9. trying to use grep to give an exact match

10. exact match

11. Exact matching?

12. Looking for a Regular Expression to do an exact match case indepenent

 

 
Powered by phpBB® Forum Software