Hi Danny, Please reply to all recipients. See below for my reply.
On Wed, 29 Jun 2016 04:35:22 +0000 Danny Wong <danny.w...@whitehatsec.com> wrote: > Hi Perl GURUs, > I have a json file that needs parsing. > Why not use a JSON parser? See http://perl-begin.org/uses/text-parsing/ . > Here is a typical string I’m searching for. I want to delete everything but > the last 2 character “],”. > Unless I'm misunderstanding something, this can be done using «my $new_string = "],";» or «my $new_string = substr($s, -2);» but neither of these seems to have a very interesting effect. > > ], > > [ > > "ansible", > > "2.1.0.0-1ppa~trusty", > > false > > ], > > > Here is what I tried: > > > I slurp the whole file into a variable. > > my $SAVE = $/; > > my $WHOLE_JSON_FILE = `cat ${JSON_FILE}`; > > $/ = $SAVE; This seems a lot like cargo-cult programming because you don't need to mess with $/ when doing backticks and `cat ... ` is not portable, not safe and may be slow. For better alternatives, see: * http://perl-begin.org/topics/files-and-directories/#string_slurp_utf8 * https://metacpan.org/pod/Path::Tiny > > > while($WHOLE_JSON_FILE !~ /.*?(\s+\]\,\s+\[\s+\"ansible\".*?)\]\,?/gs) > > { > > print "\$1 is $1"; > > } > > > The print statement is printing out the “matching string” but how do I remove > that section of string from the slurp $WHOLE_JSON_FILE variable which > contains the entire file content? > Try using the substitute operator (s///). See: http://perldoc.perl.org/perlop.html > The contents of this electronic message, including any attachments, are > intended only for the use of the individual or entity to which they are > addressed and may contain confidential information. If you are not the > intended recipient, you are hereby notified that any use, dissemination, > distribution, or copying of this message or any attachment is strictly > prohibited. If you have received this transmission in error, please send an > e-mail to postmas...@whitehatsec.com and delete this message, along with any > attachments, from your computer. Sigh. Regards, Shlomi Fish -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/