[NBLUG/talk] Another stupid Perl question....

Walter Hansen gandalf at sonic.net
Tue Jun 7 14:48:14 PDT 2005


I always use Regular Expressions for this sort of thing:

$res =~ m/SERVERSESSION=(\w.*)/;
$ss = $1;

This regular expression uses the variable $res and simply looks for a
match for the string "SERVERSESSION=" followed by zero or more word
characters (non whitespace). The parens capture that part of the string
and put it in the automatic variable $1. Now you can also put the whole
$res =~ thing in a if (*) or assign it to a variable. It returns true if
the string is matched. There's also $res =~ s/CISCO/MYCOMPANY/gi this will
search $res for cisco and replace it with mycompany through the whole
document ignoring case for cisco.

Here's a rewrite that pretty much does the same thing:

if ($res =~ m/SERVERSESSION=(\w.*)/) {$ss = $1}

They are very flexable.

I'd take a brief course on them if you don't know them. You can find one
online.

They are very, very, very powerful.




> Let's say I get a response back from a webserver and I assign that
> response
> into a variable $res
>
> $res = $ua->request($req1)->as_string;
>
> Now I have everything I want inside of that var $res.  I tried to use a
> substring function to try to grok out some values but I am missing
> something......  Tell me please...
>
> $ss = get_ss( "SERVERSESSION=\"", $res );
> ########
> sub get_ss( $$ ) {
> 	# $_[0] = string to search for, e.g., SERVERSESSION
> 	# $_[1] = string to search, e.g., $res
>
> 	return substr( $_[1], index( $_[1], $_[0] ) + length($_[0]), 18 );
> }
> ########
> --
> Mark Street, RHCE
> http://www.oswizards.com
> --
> Key fingerprint = 3949 39E4 6317 7C3C 023E  2B1F 6FB3 06E7 D109 56C0
> GPG key http://www.oswizards.com/pubkey.asc
>
> _______________________________________________
> talk mailing list
> talk at nblug.org
> http://nblug.org/cgi-bin/mailman/listinfo/talk
>
>





More information about the talk mailing list