Operating System - HP-UX
1754014 Members
6205 Online
108811 Solutions
New Discussion юеВ

regular expressions gurus

 
SOLVED
Go to solution
Jim Purtell
Frequent Advisor

regular expressions gurus

I have a string I'm trying to chop up to ruturn just a portion to the screen when called/printed.

This is the entire string.


This is what I'm trying to return.
test

How can the following code make this happen (note - I'm using the regex in the split section below).


open (TEXT_FILE, "<$WD/IqServers.xml");
while ($line = ) {
if ($line =~ m/$port/) {
$nline = ;
@nextline=split( ,$nline);

#print "$nline\n";
print "$nextline[0]\n";

close (TEXT_FILE);
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: regular expressions gurus

Hi Jim:

If all you want is the string in double quotes following the string "value=" you might do:

# X=''

# echo $X|perl -nle 'm{value="([^"]+)"} and print $1'
test

Regards!

...JRF...
Jim Purtell
Frequent Advisor

Re: regular expressions gurus

JRF

can you show me how this would be incorporated into this line? If at all possible.

@nextline=split( ,$nline);
James R. Ferguson
Acclaimed Contributor
Solution

Re: regular expressions gurus

Hi:

So it seems that $nline holds what you want to match and extract from. Hence:

...
$nline = ;
$nline =~ m{value="([^"]+)"} and print $1,"\n";

...will print the word "test" in your example.

We want to match the contents of $nline. We are looking for the string 'value="..."' and we want to extract whatever lies between an opening double quote and a closing one. If we can match this, the $1 variable holds the match and that's what we print.

Regards!

...JRF...
Jim Purtell
Frequent Advisor

Re: regular expressions gurus

JRF,
that's the answer I was looking for ..
Thank you so much!!

I'm still reading my camel book on the section of Pattern-Matching Operators though.

Regex's are a beast! But I'm still learning :-)

thanks again
Jim Purtell
Frequent Advisor

Re: regular expressions gurus

Thanks for the quick help JRF!