Trizor is correct about the dot.
For some perhaps unneeded expansion, here's one possible regex, commented, in extended notation so you can see what you're really doing (and based on what I've inferred from your use of three captures, designed to collect the protocol, IP, and server_directory):
use strict;
my $line = "http://127.0.0.1/home_dir";
if ($line =~ / # begin regex
( # begin Capture #1
http: # protocol
) # end Capture #1
\/+ # (discard) one (or more) forward slashes; impre
+cise
(127\.0\.0\.1) # Capture #2, including the dots IP
\/ # (discard) one forward slash
(home_dir) # Capture #3 (literal and simplistic)
/x) # end regex, use extended syntax, end the if pr
+edicate
{
print "$1\n";
print "$2\n";
print "$3\n";
}
else
{
print "Bad RegEx\n";
}
exit;
prints
http:
127.0.0.1
home_dir
Note that this is NOT a general solution, since I've used literals, liberally, from your example, and -- in the interest of simplicity -- without addressing such constructs as
{1,2}
a quantifier specifying either one or two of whatever it modfies <Post prandial update begins> or the use of alternate delimiters, both of which are illustrated here:
use strict;
my $line = "http://127.0.0.1/home_dir";
if ($line =~ m% # begin regex - alternate delimiter
( # begin Capture #1
http: # protocol
) # end Capture #1
/{2,2} # (discard) exactly two forward slashes
# note: no need to escape the "/" now,
# can also be written as {2} in some cases
([\d.]{1,14}) # Capture #2, including literal dots IP
/ # (discard) one forward slash
(\w{1,9}) # Capture #3, one-or-more (chars in range a-z O
+R underline)
%x) # end regex, use extended syntax, end the if pr
+edicate
{
print "$1\n";
print "$2\n";
print "$3\n";
}
else
{
print "Bad RegEx\n";
}
exit;
So why did I even bother to mention that? Well, as you learn about regexen, you can make your tools more precise and more useful... and have a lot of fun doing so.
Jeffrey E. F. Friedl's "Mastering Regular Expressions" (O'Reilly) is the canon on the topic. It's a big mouthful, but one worth chewing through a bite at a time |