Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help picking text out of string

Status
Not open for further replies.

NateBro

Programmer
Sep 1, 2004
233
US
hey, i was wondering if you guys could help me out getting some selected text out of strings.

example, if i have a string:
Code:
my $string = '<img width=12 id="as" src="myFile/img.gif" height=12>';
and i want to get pull just the picture path out..
Code:
my $string = '<img width=12 id="as" src="myFile/img.gif" height=12>';

my $img_path = 'myFile/img.gif';

how would i go about getting the var $img_path out of the $string var?

thanks for your time, and any assistance you can provide :D
~Nate_Bro
 
Nate

Try
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $string = '<img width=12 id="as" src="myFile/img.gif" height=12>';
$string =~ /src=\"([^"]+)\"/;
print $1;

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
and another, very easy to read way to do it
Code:
# look first for the string 'src="'
$string =~ /src="/;
# save just the stuff after that match
$tmp_string = $';
# then look for the "
$tmp_string =~ /"/;
# and save everything before then
$tmp_string = $`;
# $tmp_string should now contain what you want
print "$tmp_string\n";

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
[\'|\"] is better written as ["']. Because it's a character class, the pipe is unnecessary to specify OR (in fact, it will cause the regexp to match a literal pipe character, so that's not what you want). The quotation characters don't need to be escaped either, although doing so doesn't affect the operation of the regexp.
 
src attribute value may or may not have " around it.
To take that into consideration
Code:
my $string = '<img width=12 id="as" src="myFile/img.gif" height=12>';
$string =~ /src=\"?(.+?)(\"|\s)/;
print $1;

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
[\'|\"] is better written as ["']. Because it's a character class, the pipe is unnecessary to specify OR (in fact, it will cause the regexp to match a literal pipe character, so that's not what you want). The quotation characters don't need to be escaped either, although doing so doesn't affect the operation of the regexp.
Are you sure about that? Making any of the changes you are recommending ends up with an error.
Code:
String found where operator expected at ./test.pl line 7, at end of line
        (Missing semicolon on previous line?)
Can't find string terminator '"' anywhere before EOF at ./test.pl line 7.

M. Brooks
 
This works for me:
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $string = '<img width=12 id="as" src="myFile/img.gif" height=12>';

$string =~ s/<img.+?src=['"](.*?)['"].+?>/$1/gi;

print $string,"\n";
 
Oops. Missed the "disregard the last message" message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top