Extract floating point numbers from a delimited string in PHP

$str=”152.15 x 12.34 x 11mm”; preg_match_all(‘!\d+(?:\.\d+)?!’, $str, $matches); $floats = array_map(‘floatval’, $matches[0]); print_r($floats); The (?:…) regular expression construction is what’s called a non-capturing group. What that means is that chunk isn’t separately returned in part of the $mathces array. This isn’t strictly necessary in this case but is a useful construction to know. Note: calling … Read more

Saving nltk drawn parse tree to image file

Using the nltk.draw.tree.TreeView object to create the canvas frame automatically: >>> from nltk.tree import Tree >>> from nltk.draw.tree import TreeView >>> t = Tree.fromstring(‘(S (NP this tree) (VP (V is) (AdjP pretty)))’) >>> TreeView(t)._cframe.print_to_file(‘output.ps’) Then: >>> import os >>> os.system(‘convert output.ps output.png’) [output.png]:

PHP – parsing a txt file

You can do that easily this way $txt_file = file_get_contents(‘path/to/file.txt’); $rows = explode(“\n”, $txt_file); array_shift($rows); foreach($rows as $row => $data) { //get row data $row_data = explode(‘^’, $data); $info[$row][‘id’] = $row_data[0]; $info[$row][‘name’] = $row_data[1]; $info[$row][‘description’] = $row_data[2]; $info[$row][‘images’] = $row_data[3]; //display data echo ‘Row ‘ . $row . ‘ ID: ‘ . $info[$row][‘id’] . ‘<br … Read more

Split alphanumeric string between leading digits and trailing letters

You can use preg_split using lookahead and lookbehind: print_r(preg_split(‘#(?<=\d)(?=[a-z])#i’, “0982asdlkj”)); prints Array ( [0] => 0982 [1] => asdlkj ) This only works if the letter part really only contains letters and no digits. Update: Just to clarify what is going on here: The regular expressions looks at every position and if a digit is … Read more

Evaluating a string of simple mathematical expressions [closed]

Assembler 427 bytes Obfuscated, assembled with the excellent A86 into a .com executable: dd 0db9b1f89h, 081bee3h, 0e8af789h, 0d9080080h, 0bdac7674h, 013b40286h dd 07400463ah, 0ccfe4508h, 08ce9f675h, 02fc8000h, 013b0057eh, 0feaac42ah dd 0bedf75c9h, 0ba680081h, 04de801h, 04874f73bh, 04474103ch, 0e8e8b60fh dd 08e8a003fh, 0e880290h, 0de0153h, 08b57e6ebh, 0d902a93eh, 046d891dh dd 08906c783h, 05f02a93eh, 03cffcee8h, 057197510h, 02a93e8bh, 08b06ef83h dd 05d9046dh, 02a93e89h, 03bc9d95fh, 0ac0174f7h, 074f73bc3h, 0f3cac24h … Read more

Replace a whole line where a particular word is found in a text file

One approach that you can use on smaller files that can fit into your memory twice: $data = file(‘myfile’); // reads an array of lines function replace_a_line($data) { if (stristr($data, ‘certain word’)) { return “replacement line!\n”; } return $data; } $data = array_map(‘replace_a_line’, $data); file_put_contents(‘myfile’, $data); A quick note, PHP > 5.3.0 supports lambda functions … Read more

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

You can use one of the many hasNext* methods that Scanner has for pre-validation. if (in.hasNextInt()) { int a = in.nextInt() ; System.out.println(a); } else { System.out.println(“Sorry, couldn’t understand you!”); } This prevents InputMismatchException from even being thrown, because you always make sure that it WILL match before you read it. java.util.Scanner API boolean hasNextInt(): … Read more