larsen Posted May 10, 2006 Report Share Posted May 10, 2006 In a string I want to replace any sequence of blanks or tabs (\s or \t) by a single character (for instance a ; ). So a string like 123 456 78 becomes 123;456;78 irrespective if the whitespace is tabs or spaces. This should be possible to do with regular expression gymnastics, but I have not been able to figure it out. Any suggestions are wellcome. I need this to read 2D arrays of numbers from text files. Similare to the "Spreadsheet String to array" function, except that "Spreadsheet String to array" has either tabs or space as delimiter, not both. In a text file it can be hard to see the difference between tabs and spaces, and thus you may end up with both, in which case "Spreadsheet String to array" fails miserably. /henning Quote Link to comment
Jim Kring Posted May 10, 2006 Report Share Posted May 10, 2006 In a string I want to replace any sequence of blanks or tabs (\s or \t) by a single character (for instance a ; ).So a string like 123 456 78 becomes 123;456;78 irrespective if the whitespace is tabs or spaces. This should be possible to do with regular expression gymnastics, but I have not been able to figure it out. Any suggestions are wellcome. I need this to read 2D arrays of numbers from text files. Similare to the "Spreadsheet String to array" function, except that "Spreadsheet String to array" has either tabs or space as delimiter, not both. In a text file it can be hard to see the difference between tabs and spaces, and thus you may end up with both, in which case "Spreadsheet String to array" fails miserably. /henning Use the regular expression "[\s\t]+" to find the next instance of one or more consecutive space or tab characters. Do this in a loop with a shift register until you've replaced them all. Quote Link to comment
larsen Posted May 10, 2006 Author Report Share Posted May 10, 2006 Use the regular expression "[\s\t]+" to find the next instance of one or more consecutive space or tab characters. Do this in a loop with a shift register until you've replaced them all.Jim,Thanks to your precious hint I made the attached VI. Note however that I changed "[\s\t]+" into "[ \t]+" to get the newlines preserved. Note also that "[\s\t]+" is the same as "[\s]+" because "\t" is a part of whitespace "\s". But your idea works great. Every time I try to use regular expressions I learn new stuff. thanks for your help henning Download File:post-4913-1147245384.vi Quote Link to comment
malef Posted May 10, 2006 Report Share Posted May 10, 2006 Henning, string manipulations are very time consuming. If you handle large files a byte conversion with ascending manipulation will save a lot of computing time. Manfred Download File:post-831-1147260271.vi Quote Link to comment
larsen Posted May 10, 2006 Author Report Share Posted May 10, 2006 Manfred, Thanks for your elegant suggestion. Speed is however not a concern in my particular case. Some other time maybe. I tried your vi, but there is a small bit of cleaning up to do though. Could not see how to fix this however, but maybe you can, having it fresh in mind: Currently it removes double characters, which was not what I had in mind: 22 33 becomes 2;3 and not 22;33 It should only slim down duplicated blanks and tabs. /henning Quote Link to comment
Jim Kring Posted May 10, 2006 Report Share Posted May 10, 2006 Jim,Thanks to your precious hint I made the attached VI. Note however that I changed "[\s\t]+" into "[ \t]+" to get the newlines preserved. Note also that "[\s\t]+" is the same as "[\s]+" because "\t" is a part of whitespace "\s". But your idea works great. Every time I try to use regular expressions I learn new stuff. thanks for your help henning Henning. Search and Replace does not use regular expressions. Also your interpretation of \s and \t is not correct, with respect to their usage in regular expressions: \s is the space character (not all whitespace) \t is the tab character \n is the line feed \r is carriage return ...and, there are some others. Please see the attached example, which meets your requirements. Download File:post-17-1147276416.vi Another (and probably best) option is to use Search and Replace Pattern.vi. Quote Link to comment
larsen Posted May 10, 2006 Author Report Share Posted May 10, 2006 Henning. Search and Replace does not use regular expressions. Also your interpretation of \s and \t is not correct, with respect to their usage in regular expressions:\s is the space character (not all whitespace) \t is the tab character \n is the line feed \r is carriage return hello Jim, I think there is a confusion as to what a regular expression is. Search and replace String.vi has both modes. Selected by right-click on it. Take a look at the attached example comparing behaviour of Search and replace String with Search and replace pattern. Both claim to have regular expression inputs (if so selected, at least in LV8), but result differs for regular expression = [\\s\\t] (in \code display) = [\s\t] (as a normal string). Search and replace String.vi: With regular expression= [\\s\\t], string \s\t\n\r is matched in all 4 characters (all written in \ code display) Search and replace Pattern.vi: With regular expression=[\\s\\t], string \s\t\n\r is matched only in blank_space and tab characters In addition its not always clear what \s actually means: In \ code display it is ascii 0x20 whereas in normal display its acsii 0x5C 0x73. Best I can say is that its easy to misunderstand and misuse. henning Download File:post-4913-1147297019.vi Quote Link to comment
Jim Kring Posted May 11, 2006 Report Share Posted May 11, 2006 ...Search and replace String.vi has both modes. Selected by right-click on it.... OK, that's new for LabVIEW 8.0. It's really starting to frustrate me how many of these hidden little gotchas NI has started putting into primatives. Adding right-click options to nodes is such an awful way of circumventing the 4-2-2-4 conpane standard. At least with Search and Replace String the function icon looks slightly different (having an RE "pattern" asterisk), but the Read Text File function has similar options for conveting EOL characters and this just drives me nuts. Who is going to think to right click on a node to see how its configured? These are worse than Node Setup Options. A firm wag of the finger to whoever decided to add right-click options to nodes, especially without any distinct visible change of the icon Quote Link to comment
Darren Posted May 11, 2006 Report Share Posted May 11, 2006 LabVIEW R&D is aware of this frustration regarding right-click options on functions. We are planning on addressing this issue in a future LabVIEW version. Thanks for the feedback...just wanted to let y'all know that we're listening... -D Quote Link to comment
malef Posted May 11, 2006 Report Share Posted May 11, 2006 ... but there is a small bit of cleaning up to do though. ... Currently it removes double characters ... Sorry Henning, I fixed faulty VI and simplified it. Manfred Download File:post-831-1147336333.vi 1 Quote Link to comment
torekp Posted May 11, 2006 Report Share Posted May 11, 2006 Manfred, I tweaked your VI to make it even faster. At least, it's faster with really large strings (I used a large text file to test with). I'm throwing in the tester VI. The difference is, I initialized the array shift register with the original string (as byte array) and used replace array element (at index i) instead of append to array. You also can use this trick when you are deleting elements instead of replacing them - just be sure to keep track of the modified array length, and take a subset after the loop. Download File:post-4616-1147350812.viDownload File:post-4616-1147350375.vi Edit: revised to eliminate the bug pointed out by larsen Quote Link to comment
PJM_labview Posted May 11, 2006 Report Share Posted May 11, 2006 LabVIEW R&D is aware of this frustration regarding right-click options on functions. We are planning on addressing this issue in a future LabVIEW version.Thanks for the feedback...just wanted to let y'all know that we're listening... -D This is good to know, as I waste a LOT of time figuring out why I could not read back file saved with LV 7.1 using the overloaded LV 8.0 "read from text file" primitive (which has two right click option which means it has 4 modes of operation and the same icon for all 4!). So, I am another very frustated user in that regard. PJM Quote Link to comment
lavezza Posted May 11, 2006 Report Share Posted May 11, 2006 OK, this is off-topic from the original post but it has to do with right-click options for primitives. There is one that has been in there for a while that just recently bit one of our new programmers. He was using the Array-to-Cluster primitive. By coincidence it was an array with 9 values and everything worked fine. When he needed to expand it to 10 values he couldn't figure out what was wrong. The array had 10 values and the cluster was expanded to have 10 values but there were broken wires. Of course, you need to set the cluster size by right-clicking on the Array-to-Cluster primitive. There are others as well. I've seen new programmers that need to compare arrays not realize that you can set the comparison mode to "Compare Aggregates". Maybe some of these primitives could get visual indicators (small ones!) that indicate their state. Pat Quote Link to comment
JDave Posted May 12, 2006 Report Share Posted May 12, 2006 Maybe some of these primitives could get visual indicators (small ones!) that indicate their state. Amen to that. Quote Link to comment
Phillip Brooks Posted June 5, 2006 Report Share Posted June 5, 2006 Speak up now! New Feature Brainstorming Topic HERE I especially like the 3-D look comment... Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.