08-09-2012 03:37 AM
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
Solved! Go to Solution.
08-09-2012 08:58 AM - edited 08-09-2012 09:04 AM
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'.
08-09-2012 09:20 AM
Perfect.
Thank you.
11-17-2014 10:54 AM
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.
11-17-2014 11:08 AM
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.
11-17-2014 11:23 AM
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 '?'
11-17-2014 12:31 PM
@Darin.K wrote:
I am not going to quibble over the choice of submatches versus assertions and other alternatives [...]
I was hoping someone would.
11-19-2014 04:03 AM
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?
11-19-2014 07:52 AM
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.
11-19-2014 07:57 AM