> -----Original Message-----
> From: Dan Finch [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, July 17, 2002 12:03 PM
> To: [EMAIL PROTECTED]
> Subject: Pattern Matching
>
>
> I'm working on a script that I need to use some pattern
> matching in. I've
> got a couple books that I have been reading about it and
> thought I knew how
> to seach for what I'm after but its just not working.
>
> Here is a test script I've been playing with to get this
> working. I want to
> be able to parse out of the string any one of the
> 'letter=values'. The way
> I'm trying to get this to work is by searching for the Letter of the
> parameter I'm searching for follow by the =. Then I add a .*
> which matches
> anything. Then I want to match to end with a '
>
> But no matter what I've tried I will never find the ' It
> find the beginning
> of my search then go to the end of the string. I've played
> with escaping
> the ' but that didnt seem to work either.
>
> What I end up having to do to get it to work is end the seach with the
> Letter= of the next variable in the string and then clean it
> up but I really
> don't want to have to do it that way as its not very flexable
> and relies on
> the string being in a certain order and I don't want to count on that.
>
> If anyone has an idea about what I'm doing wrong I would be
> most grateful.
>
>
>
> #!/usr/bin/perl
>
> $test = "'A=test' 'P=lkjae' 'p=12' 'H=goober@localhost' 'n=crfrinch'
> 't=3453462'";
There's a subtle bug here that "use strict" would catch. The "@localhost"
is silently being expanded to the contents of the array @localhost, since
you are using double quotes. Since this array doesn't exist, this is
expanding to an empty string, i.e. 'H=goober'.
Adding "use strict" flags the error, because you did not declare an array
called "@localhost".
To avoid the expansion, you need to escape the @ sign or use the q[]
quoting construct:
use strict;
my $test = "'A=test' 'P=lkjae' 'p=12' 'H=goober\@localhost' 'n=crfrinch'
't=3453462'";
or,
use strict;
my $test = q['A=test' 'P=lkjae' 'p=12' 'H=goober@localhost' 'n=crfrinch'
't=3453462'];
> $_ = $test;
> /(n=.*')/;
The * is "greedy", which means it will match the *longest* sequence
up to a "'" char. This will match the string:
n=crfrinch' 't=3453462'
^ this is last "'" that can be matched
> $username = $1;
This is bad, because $1 will not be changed if the regex did not match.
Never use $1 without checking that the regex succeeded.
> print "$1\n";
Here's an example that will print all the pairs:
#!/usr/bin/perl
use strict;
my $test = q['A=test' 'P=lkjae' 'p=12' 'H=goober@localhost' 'n=crfrinch'
't=3453462'];
print "$1 => $2\n" while ($test =~ /'(.*?)=(.*?)'/g);
output:
A => test
P => lkjae
p => 12
H => goober@localhost
n => crfrinch
t => 3453462
The .*? construct is the "non-greedy" version of .* and matches the
*shortest* possible string (i.e. up to the *first* occurrence of the
next token).
The /g modifier is used when matching in a loop, to cause the match to
start from where the last match ended.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]