Posted by Jason Turner on September 18, 1998 at 17:16:59:
In Reply to: Re: Perl string manipulation posted by Jason Nugent on September 18, 1998 at 12:48:43:
: A space?? Man... sorry about that. Ok, I didn't see it :)
HTML does horrible things to spaces. Believe me, that code I wrote was actually readable b4 it was posted, <g>
: Good point about the password field. This all comes back to knowing your data. Instead of using a \w+ (which matches alphanumerics), you might consider using a [^\|]+ instead, which should match everything except |. Or just make your users use bad passwords :)
Users don't need any encouragement to make bad passwords, they do fine as it is <g>
BTW, I know you meant [^|]+ really ;-)
You gotta be careful of that though - see the method used to create $new, and then the explanation on how it broke on the null string. If that space is always there then it's not so much of a problem - like you say, you gotta know your data.
That little chunk of code I posted is great at showing the comparisons, but you really gotta run it to see what effect the subtle changes have, and to follow what I was saying in the notes.
OK, I s'pose I should have put a few more comments in <g>
Anyway, one of them that Bob could cut and paste was the example for $new; adapted a little to throw away that trailing space, if present.
$pish =~ s/.*\| ([^|]*) ?\|$/$1/;
Oh, and that is distructive, where the first example wasn't.
I'd probably use $new myself and avoid regex altogether
$password = ( split '\|' => $pish, -1 );
chop $password if $my_data_always_has_space;
I love these Perl threads :)