regex - Why is a regular expression containing the exact string not matching successfully? -


this might beginners mistake.
regex turns out not matching while should.

#!/usr/bin/perl  # print "hello, world" print "hello, world\n"; $addr = "hello"; #if($addr =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\)/ ) if (my $addr =~ /hello/) {       print("matched\n\n"); }else {   print("didnt match\n\n"); } 

the my makes variable match local , uninitialised. should change

if ($addr =~ /hello/)

the my indicates $addr in if "my own here", i.e. different other $addr 1 larger, outer scope.
outer scope variable got initialised match regex. second, inner 1 not initialised , (at least in case) has no matching value.

note: comments other authors have proposed best practice avoiding/detecting cause of problem in future programming.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -