473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Extract a substring between two elements

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
6 17213
nithinpes
410 Expert 256MB
What did you try? Post the regex that you tried.
Nov 12 '08 #2
tcouto
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
4,059 Expert 2GB
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
3,652 Expert 2GB
Or possibly:

Expand|Select|Wrap|Line Numbers
  1. my $url =~ m#//(.*?)/#;
  2. my $blog_url = $1;
Nov 12 '08 #5
eWish
971 Expert 512MB
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
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

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

Similar topics

1
by: Tim Smith | last post by:
I am looking to extract form element values from html, more generally I have a substring that identifies the beginning of a value and a string that identifies the end of value and I need to extract...
4
by: rdraider | last post by:
Is there a function that will extract part of a string when the data you want does not occur in a specific position? Field "REF" is varchar(80) and contains an email subject line and the email...
9
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
3
by: jarod1701 | last post by:
Hi, I'm currently trying to create a regular expression that can extract certain elements from a url. The url will be of the following form: http://user:pass@www.sitename.com I want a...
1
by: Nick | last post by:
Hi, I'm trying to extract element of a directory path stored in the db with substring "/help/support/index/time.jsp" and i want to extract the 1st, 2nd and 3rd parts 1st = help, 2nd =...
1
by: Patrick Sullivan | last post by:
I am trying to extract two parts of a number from an array element. Numbers are in the format of 1.10, 2.50, 11.10, etc. Floor and ceiling won't work right because close to 1.00, I get a zero, and...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
13
by: Tony Girgenti | last post by:
Hello. Using VS.NET 2003 VB. If i have a string similar to the attached, how would i extract the "Truckname=" data from it in a loop and stay in the loop until the end of the string is reached...
5
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.