1. There is a missing semicolon on the first line.
2. Does "$string" really contain line breaks? If it does you need the
"s" option in the regex.
how about this;
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
use strict;
my $string = '<Name><FN>John</FN> <LN>Smith</LN></Name>,
<Name><FN>Susan</FN> <LN>Miers</LN></Name>
<JobTitle>President</JobTitle>'; # missing semicolon
# assuming $string may contain multiple lines
if ( $string =~ m/.*<Name>(.+?)<\/Name>.*<JobTitle>(.+?)<\/JobTitle>/s ) {
my $personName = $1;
my $jobTitle = $2;
print qq{personName==$personName==\n};
print qq{jobTitle==$jobTitle==\n\n};
$personName =~ s/<[^>]+>//gs; # remove extra tags
$personName =~ s/\s\s+/ /gs; # clean up the whitespace
print qq{personName==$personName==\n};
print qq{jobTitle==$jobTitle==\n\n};
} else {
print "No name found in ==$string==\n"; # oops
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Which gives;
personName==<FN>Susan</FN> <LN>Miers</LN>==
jobTitle==President==
personName==Susan Miers==
jobTitle==President==
Hsu, David wrote:
Hi,
I am trying to parse this string and not able to get the results that I
need. Can someone please help. Thanks in advance. -David
I am trying this:
$string = '<Name><FN>John</FN> <LN>Smith</LN></Name>,
<Name><FN>Susan</FN> <LN>Miers</LN></Name>
<JobTitle>President</JobTitle>'
$string =~ m/.*(<Name>.+?<\/Name>).*(<JobTitle>.+?<\/JobTitle>)/;
I only want the name in last <Name> tag and the <Jobtitle> (i.e. Susan
Miers President)
Also, the <FN> and <LN> tag may or may not be there at times.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs