LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do a parse all strings that match a regexp from a text file?

I need help trying to write a VI to extract all strings that match the regexp "##>+.#<" from a text file.

 

In perl, sed, awk, basic, c, c++, java, assembly language, hand writting machine code I can do this in from 5 to 30 minutes(hand coded machine code!).

 

But in LabVIEW I have spent over 12 hours today and I am ready to throw the computer against the wall!!!!!!!!

 

Why the bloody hell is this so damn hard to do!!!!

 

Open a file

read it to a multi-line string

extract every string the matches regexp(>##+.#<)

 

done!

 

 

I have attached a sample of the file I am trying to parse and the vi I have been  using to try and figure this out.

 

The goal is to strip out all the lines that match regexp(>##+.#<).

 

In the test program I can type in a specific line number and the regexp works.  I can also passed the entire file string to the regexp method and it finds the first occurance only!

 

I have two options

 

1. get the part of the code that parses the entire file in one chunk working (preferred)

2. iterate through every line of the file matching the regexp.

 

The problem I am having is I am a total labview newb and I cannot figure out how to iterate line-by-line through a multi-line string and I cannot figure for the life of me why the part of the code that passes the entire file string to the regexp method only finds the first occurance.  IT"S MADDENING! 

 

 

 

 

ARGH!!!!! It will not let me attached files!!!! 

0 Kudos
Message 1 of 15
(3,377 Views)
small sample of data file.  Should output two lines the match >##+.#<
0 Kudos
Message 2 of 15
(3,376 Views)

The VI I having been using to try in figure this out in LabVIEW....

 

 

The damn forum will not let me upload a .vi file,   I have tried sending it as .vi, .rar, .zip......it keeps saying "ujnexpected error has occured"  !!! 

0 Kudos
Message 3 of 15
(3,375 Views)
another try at uploading the test VI I have been using to figure this problem out.
0 Kudos
Message 4 of 15
(3,374 Views)

I can't look at your VI at the moment as I don't have 8.6 running, but based on looking at your file you seem to want to parse an XML file. LabVIEW 8.6 has built-in XML functions.  Wouldn't those be easier to use?

 

As for your regular expression, I don't quite understand it. What are you trying to find within the sample text that you showed? Describe it in words.

 

P.S. VIs get uploaded all the time. Most likely you are running a browser that has problems with the forum software. For example, Google Chrome has a few issues

Message Edited by smercurio_fc on 03-11-2009 05:20 PM
0 Kudos
Message 5 of 15
(3,356 Views)

No it looks like XML but it is not so I cannot use the XML parsing tools on it.  I had to put the comments in and the project requirements are that a LabVIEW VI has to be created to pull the comments out and then parse those into valid XML.

 

I am dreading having to use LabVIEW to parse the strings once I pull them out.  

 

I am sure it is because I am completely new to LabVIEW but I find it incredibly complicated to do the simplest of string manipulations in LabVIEW.  But alas I am force to learn it. 

0 Kudos
Message 6 of 15
(3,336 Views)
Can you post an example of text and how your Regex key will match it?  I also don't follow your example.  It seems that if you have the correct regex defined then parsing should be the easy part.  Once I understand your regex I can help you do the parsing.
0 Kudos
Message 7 of 15
(3,334 Views)

Oh I forgot to describ what I am searhing for in the sample....

 

The comments inside the <DESC></DESC> tags that has the >##{stuff}#< format.

 

I have to strip those strings out.  Then turn them into valid XML structure.  The sample txt only has two comments but the full file will have dozens.

 

The tag format is ##<state>[:<substate>:<substate>...]#<tag>#<tag name># and some other minor variations that I can hopefully parse back to the required XML.

 

The regular expression >##.+#< will extract all the strings I need.  I was hoping I could send a single multi-ling string to the regexp method and dump all the matches in one go but as hard as I tried I had to give up and create a while loop reading a string at a time until I read a "" string.  It's cludgy as hell but I need to get this done by tomorrow  so I dont have time to become an expert at LabVIEW.

 

Thank you for any advice. 

0 Kudos
Message 8 of 15
(3,333 Views)

The regexp is pretty simple.  It is just matching the >##{then everything in here up to}#<, so in the sample file I'd expect to get the follow two lines out. 

 

>##Start#Next_State#State 1#< 

>##State 1#Next_State#State 1#< 

 

Which will get turned into ...

 

<Items>

<Start Id=0 name="Start" /> <--- these are the only comment tags in the small sample

<State Id=1 name="State 1"> <--- so it is hard to see the structure I am trying to pull out

<Items>

...State 1 stuff...

</Items>

</State> 

 

...other states

...connections 

... 

<Items> 

0 Kudos
Message 9 of 15
(3,330 Views)

I am sure the regexp is fine.  Here is another version  of the VI I am using to figure things out.  Sorry for it being so sloppy.  I started thinking I could pass the entire multi-line file to regexp (all the stuff below the while loop), but that part only finds the first match.  Tfhe stuff in the while loop reads the file string one line at a time and sends it to the regexp method and then displays the match if there is one.  I out a timer in to see that it was working.  What I really want is to just print each of the matches to another file or string array so I can parse them further.

 

My problem is my head is still in object / procedural programming mode and I am sure I am doing everything wrong in LabVIEW. 

0 Kudos
Message 10 of 15
(3,329 Views)