Reputation: 61
I want to find a string that begins with http://
and ends with .com
.
but the http://
and .com
it doesn't need to be printed.
$str = "http://example.com";
$str =~ /http:\/\/example.com/;$result = "$&\n";
print $result;
essentially the same as that done with python.
#!/usr/bin/python
import re
str = 'http://example.com'
search = re.search(r'http://(\w+).com', str)
if search:
print search.group(1)
it will only show "example". How to do it in Perl?
Upvotes: 0
Views: 283
Reputation: 39158
Robust solution with a specialised parser:
use feature 'say';
use strict; use warnings;
use URI;
use URI::Find;
URI::Find->new(sub {
my $uri = shift;
say $uri->host =~ m{(\w+)[.]com\z};
})->find(\ (my $x = q{http://example.com/}) );
Upvotes: 3
Reputation: 43673
Try this simple code:
$str = 'http://example.com';
print "$_\n" for $str =~ m{\A http:// (\w+) [.] com \z}x;
To ensure your result is complete, anchor the pattern at the beginning , \A
, and end, \z
. Use a different pattern delimiter than /
to avoid the leaning toothpick syndrome, and use the x
option to make your pattern more readable.
You need to use (...)
to capture the part you want to extract.
You can test this code on ideone.com
Upvotes: 0
Reputation: 212
Not so perlish solution below:
$str = 'http://example.com';
if (($url) = $str =~ /http:\/\/(\w+)\.com/) {
print $url, "\n";
}
Upvotes: 0
Reputation: 8376
In your Python snippet you're capturing the text you want with parentheses, but in your Perl snippet you've left them out. Also, the part you want to capture is hard-coded instead of expressed as \w+
. Dig there.
Upvotes: -1