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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching

Status
Not open for further replies.

Buddyholly83

IS-IT--Management
Aug 7, 2005
23
GB
I am being returned some data that looks like:
Code:
Set-Cookie: ASP.NET_SessionId=bir2jo45j0jev355wqdp0xed; path=/
X-AspNet-Version: 1.1.4322
X-Powered-By: ASP.NET

Result=OK
MessageIDs=f89fcc9b-b7ba-44a8-bebe-785302b6f136
The bit i am interested in is the Result and messageIDs lines. I can successfully find these lines using:
Code:
my ($string) = @_;
	my $response = undef;
	
	if($string =~ m/Result=Test/)
	{
		#Return nothing
		$response = "Test worked\n";
	}
	
	if($string =~ m/Result=OK/)
	{
		#Return MessageID value
		$response = ($string =~ m/MessageIDs=/../eof()/);
	}
But what my code is doing is checking if the string contains 'MessageIDs={..}' in the data and it sets $reponse = 1. I infact want the long code part, i.e. i want f89fcc9b-b7ba-44a8-bebe-785302b6f136

How do i extract this ?

Thanks :)
 
You want everything from `MessageIDs=' to the end of the line, right?

Code:
$response = ($string =~ m/MessageIDs=(.*)$/);
 
I want just the long string. So if the line is:
Code:
MessageIDs=f89fcc9b-b7ba-44a8-bebe-785302b6f136
I want this bit:
Code:
f89fcc9b-b7ba-44a8-bebe-785302b6f136
The bit between 'MessageIDs=' and end of line.

This still returns '1' rather than 'f89fcc9b-b7ba-44a8-bebe-785302b6f136'
 
Thanks for your input ishnid.

All I needed to do was access the code via $1 variable:

Code:
#Return MessageID value
		$string =~ m/MessageIDs=(.*)$/;
		$response = $1;
 
Ah. I was missing parentheses from my original post. Should have been:
Code:
( $response ) = ($string =~ m/MessageIDs=(.*)$/);
That's a little safer than the above. If the regexp doesn't match, $response isn't assigned anything. With your one above, $response will be set to the first capturing group of the last regexp that *did* match, regardless of whether it was the one on the previous line or not. You could alternatively introduce an 'if' statement to get the same effect.
 
Hi Buddyholly83

ishnid is making a noteworthy point - be very careful with the way you have gone about it - i have been caught out several times going about it the way you are just about to...


Kind Regards
Duncan
 
alternative method, assumes no "=" in the value of messageID:

Code:
sub my_sub {
   my $string = shift;
   my $response = "Test worked\n"; 
   $response = (split(/=/,$string))[1] if ($string eq 'Result=OK';
   return($response);
}
 
Thanks for the pointers guys. It has been all taken onboard! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top