For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > regex matching









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regex matching
cknipe@savage.za.org

2006-05-31, 8:00 am

Hi,

if ($_ =~ m/match string/i) {
if ($_ =~ m/does not match string/i) {
} else {
print $_;

Regex is not my strong point, so I'm going to ask... Is there any way to write
that better? Preferably only using one if statement?

if (($_ =~ m/match string/i) && ($_ =~ m/does not match string/i)) {
print $_;

I'm not to sure how to do the reverse of =~ for the does not match part...

Thanks,
Chris





cknipe@savage.za.org

2006-05-31, 8:00 am

if (($_ =~ m/match string/i) && ($_ !~ m/does not match string/i)) {

Works flawlessly, thanks allot...

--
Chris


Quoting Shashidhara Bapat <shashidharasbapat@gmail.com>:

> Hi,
>
> yes you can do that. For "not match", you got to use "!~".
>
> - shashi
>
> On 5/31/06, cknipe@savage.za.org <cknipe@savage.za.org> wrote:
>




Mr. Shawn H. Corey

2006-05-31, 8:00 am

On Wed, 2006-31-05 at 12:03 +0200, cknipe@savage.za.org wrote:
> Hi,
>
> if ($_ =~ m/match string/i) {
> if ($_ =~ m/does not match string/i) {
> } else {
> print $_;
>
> Regex is not my strong point, so I'm going to ask... Is there any way to write
> that better? Preferably only using one if statement?
>
> if (($_ =~ m/match string/i) && ($_ =~ m/does not match string/i)) {
> print $_;
>
> I'm not to sure how to do the reverse of =~ for the does not match part...


The inverse of '=~' is '!~'
See `perldoc perlop`


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


niall.macpherson@ntlworld.com

2006-05-31, 8:00 am

cknipe@savage.za.org wrote:

>
> I'm not to sure how to do the reverse of =~ for the does not match part...
>
> Thanks,
> Chris


For not match you would use the !~ operator

e.g.

if ($string !~ /cat/)
{
print 'String does not contain cat';
}

There shouldn't be a situation where you would want do something like

if($string =~ /cat/ && $string !~ /catapult/)
{
print 'String matches cat but not catapult';
}

since you could quite easily construct a single regular expression
which would suit your needs rather than testing a match and a
non-match.

Hope this helps

niall.macpherson@ntlworld.com

2006-05-31, 8:00 am


niall.macpher...@ntlworld.com wrote:

>
> There shouldn't be a situation where you would want do something like
>
> if($string =~ /cat/ && $string !~ /catapult/)
> {
> print 'String matches cat but not catapult';
> }
>
> since you could quite easily construct a single regular expression
> which would suit your needs rather than testing a match and a
> non-match.
>


Here's one way you could do it (as usual TIMTOWTDI) using capturing
parentheses (untested)

if($string =~ /cat(\S)/)
{
if($1 eq 'apult')
{
print 'It has a cat but I do not want catapult'
}
else
{
print "I do want $string";
}
}
else
{
print "Do not want $string as it does not contain cat";
}

John W. Krahn

2006-05-31, 8:00 am

cknipe@savage.za.org wrote:
> Hi,


Hello,

> if ($_ =~ m/match string/i) {
> if ($_ =~ m/does not match string/i) {
> } else {
> print $_;


According to that logic:

$ perl -le'
for ( "abcdefgh", "rstuvwxyz", "jklmnop", "abcdefwxyz" ) {
if ( /cde/i ) {
if ( !/xyz/i ) {
} else {
print
}
}
}
'
abcdefwxyz

You want:

if ( /match string/i && /does not match string/i ) {
print;
}


> Regex is not my strong point, so I'm going to ask... Is there any way to write
> that better? Preferably only using one if statement?
>
> if (($_ =~ m/match string/i) && ($_ =~ m/does not match string/i)) {
> print $_;
>
> I'm not to sure how to do the reverse of =~ for the does not match part...


Or is this what you really want:

$ perl -le'
for ( "abcdefgh", "rstuvwxyz", "jklmnop", "abcdefwxyz" ) {
if ( /cde/i && !/xyz/i ) {
print
}
}
'
abcdefgh



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com