Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to match String with Case Sensitive??

by Sachin (Acolyte)
on Oct 30, 2009 at 11:29 UTC ( [id://804132]=perlquestion: print w/replies, xml ) Need Help??

Sachin has asked for the wisdom of the Perl Monks concerning the following question:

Anybody can tell me any method to match Case Sensitive String. I wouldnot like to convert the $test1 in lowercase. I want to match as it is.

#! /usr/bin/perl my $test = "abc"; my $test1 = "AbC"; if($test =~ m#$test1#i){ print "\n Both are Same"; } else{ print "\n Both are not Same"; }

Replies are listed 'Best First'.
Re: How to match String with Case Sensitive??
by moritz (Cardinal) on Oct 30, 2009 at 11:46 UTC

    If you want case sensitive matching, just remove the i at the end.

    In any case your test will return true if $test1 is a substring of $test, and if it contains special regex syntax it's something else completely. So you could do

    if ($test =~ m/^\Q$test1\E\z/) { ... }
    instead to avoid that.
    Perl 6 - links to (nearly) everything that is Perl 6.

      Thansk for Reply. I removed the i at the end. But it is not matching.

      #! /usr/bin/perl my $test = "abc"; my $test1 = <STDIN>; # Suppose test1 = "AbC" if($test =~ m#$test1#){ print "\n Both are Same"; } else{ print "\n Both are not Same"; }
        That's because the line terminator, "\n" is at the end of $test1.
        chomp($test1);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://804132]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found