LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular expression help please. (extracting a string subset between two markers)

Solved!
Go to solution

I haven't used regular expressions before, and I'm having trouble finding a regular expression to extract a string subset between two markers.

 

The string;

 

Header stuff I don't want
Header stuff I don't want
Header stuff I don't want

Header stuff I don't want
Header stuff I don't want
Header stuff I don't want

ERRORS 6
                     
Info I want line 1
Info I want line 2
Info I want line 3
Info I want line 4
Info I want line 5
Info I want line 6
END_ERRORS


From the string above (this is read from a text file) I'm trying to extract the string subset between ERRORS 6 and END_ERRORS. The number of errors (6 in this case) can be any number 1 through 32, and the number of lines I want to extract will correspond with this number. I can supply this number from a calling VI if necessary.

 

My current solution, which works but is not very elegant;

 

(1) uses Match Regular Expression to the return the string after matching ERRORS 6

(2) uses Match Regular Expression returning all characters before match END_ERRORS of the string returned by (1)

 

Is there a way this can be accomplished using 1 Match Regular Expression? If so could anyone suggest how, together with an explanation of how the regular expression given works.

 

Many thanks

Alan

0 Kudos
Message 1 of 14
(6,144 Views)
Solution
Accepted by topic author AlanKnowler

 

Example_VI.png

 

I used a character class to catch any word or whitespace characters.  Putting this inside parentheses makes a submatch that you can get by expanding the Match Regular Expression node.  The \d finds digits and the two *s repeat the previous term.  So, \d* will find the '6', as well as '123456'.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 2 of 14
(6,116 Views)

Perfect.

 

Thank you.

0 Kudos
Message 3 of 14
(6,096 Views)

What expression should a string like the following would need to match just between '!' and '?' ?

 

blabla! DAT 4500.000 ?blabla

 

Is there any way I can filter this string right on the first reading?

 

Thank you in advance.

0 Kudos
Message 4 of 14
(5,614 Views)

reg.png

 

I put the Trim Whitespace in to clear out the spaces at the front and back.  Submatches are an easy way to get your piece in one shot.  Other folks might suggest better regular expressions that'll get what you want as the whole match.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 5 of 14
(5,604 Views)

I am not going to quibble over the choice of submatches versus assertions and other alternatives, but I typically like to enforce the grammar using the regex, in this case that means adding the closing '?' to the regex.  You can leave it outside the submatch, but I would prefer having something there to catch malformed strings without a closing '?'

Message 6 of 14
(5,597 Views)

@Darin.K wrote:

I am not going to quibble over the choice of submatches versus assertions and other alternatives [...]


I was hoping someone would.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 7 of 14
(5,576 Views)

Thank you! it has been very useful so far!

 

If somehow I have the information in several lines like:


blablabla!DAT 2?blablabla
blablabla!ISSUE 0.88?blablabla
blablabla!FLAG 0?blablabla

 

How can I match different submatches?

0 Kudos
Message 8 of 14
(5,543 Views)

Put it in a loop.  Keep track of the "offset past match" in a shift register so you can start past the last match.  Index the results into an array outside the loop and you've got them all.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 9 of 14
(5,524 Views)
Thank you! 😉
0 Kudos
Message 10 of 14
(5,521 Views)