By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Extract a substring between two elements

P: 2
Hello there

I'm new with Perl... I need to extract a substring between two elements.
I'm working with urls and I need to get the text between "http://" and the first "/" in that string.

For example, in the following string:
Expand|Select|Wrap|Line Numbers
  1. http://www.example.com/test/index.html
I want to retrieve
Expand|Select|Wrap|Line Numbers
  1. www.example.com
How can I do that? Thanks!!!
Nov 12 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
What did you try? Post the regex that you tried.
Nov 12 '08 #2

P: 2
I'm currently removing the "http://" with the substring function, since it's a constant number of characters
Expand|Select|Wrap|Line Numbers
  1. sub get_blogname{ 
  2. my ($blogurl) = @_;
  3. my $blogname = substr($blogurl, 7);
  4.  
  5. return $blogname;
  6. }
I just don't know how to retrieve all the characters in the $blogname until the first "/" appears
Nov 12 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
one possibility:

Expand|Select|Wrap|Line Numbers
  1. sub get_blogname{ 
  2.    my ($blogurl) = @_;
  3.    my ($blogname) = $blogurl =~ m|^(?:http://)?([^/]+)|i;
  4.    return $blogname;
  5. }
Nov 12 '08 #4

Ganon11
Expert 2.5K+
P: 3,652
Or possibly:

Expand|Select|Wrap|Line Numbers
  1. my $url =~ m#//(.*?)/#;
  2. my $blog_url = $1;
Nov 12 '08 #5

eWish
Expert 100+
P: 971
Unless the data you are interested in is fixed length then substr is not the way to go. I would suggest that you use one of the other suggestions. However, if it is a fixed length the substr would be fine.

Expand|Select|Wrap|Line Numbers
  1. my $url = 'http://www.somedomain.com/somepage.html';
  2. print substr($url,7,18);

Or in a subroutine.
Expand|Select|Wrap|Line Numbers
  1. print &get_url('http://www.somedomain.com/somepage.html');
  2.  
  3. sub get_url {
  4.     croak "You did not pass the proper number of arguments\n" if @_ != 1;
  5.     return substr($_[0],7,18);
  6. }
--Kevin
Nov 13 '08 #6

P: 3
We can do this way also

Expand|Select|Wrap|Line Numbers
  1. $text = "http://www.example.com/test/index.html";
  2.  
  3. $text =~s/http:\/\///i;
  4.  
  5. $split1 = (split /\//,$text)[0];
  6. print $split1;
Check this out
Nov 14 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.