ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Multi-line String - Match Regular Expression

Solved!
Go to solution

I am trying to figure out the format of a regular expression in order to pull select lines out of a multi-line string and populate those lines as individual elements of a string array while using Match Regular Expression. The overall length of the multi-line string can vary as well as the text contained within the string. The string can contain letters, numbers, and special characters. I have attached an example VI. Within the example VI I only want to return the lines beginning with "Device #" into the array. The number of lines beginning with "Device #" can vary but I want to capture them all.

 

Or is there a better function to use instead of Match Regular Expression that will give me the desired outcome?  

0 Kudos
Message 1 of 6
(6,971 Views)

Slight change to my example VI attached. This one gives me the match but it does not give the match + the rest of the line.

0 Kudos
Message 2 of 6
(6,960 Views)
Solution
Accepted by topic author aaronb

@aaronb wrote:

I am trying to figure out the format of a regular expression in order to pull select lines out of a multi-line string and populate those lines as individual elements of a string array while using Match Regular Expression. The overall length of the multi-line string can vary as well as the text contained within the string. The string can contain letters, numbers, and special characters. I have attached an example VI. Within the example VI I only want to return the lines beginning with "Device #" into the array. The number of lines beginning with "Device #" can vary but I want to capture them all.

 

Or is there a better function to use instead of Match Regular Expression that will give me the desired outcome?  


Match Regular Expression works well for this.

 

multiline regex solution.png

 

Ben64

Message 3 of 6
(6,959 Views)

Thanks that works. I guess in my example I was missing the "+ .*" in the regex to include the rest of the line (thats the part I was trying to figure out)

 

Is there any drawback or difference when using the while loop example I have in the attached VI compared to your For loop example? Your For loop example seems cleaner and I will probably use that but I am trying to understand if there are any negatives in the While loop version.

0 Kudos
Message 4 of 6
(6,949 Views)

The way you implemented it it will stop when a Device n is not found even if a Device n+1 exist. It will also capture "Device n" even if it is not at the start of a line. By changing the string to an array of string you can use "^" to anchor the match at the beginning of a line.

 

Ben64

0 Kudos
Message 5 of 6
(6,935 Views)

OK thanks for all your help!

0 Kudos
Message 6 of 6
(6,923 Views)