LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Search and Replace String throwing the wrong error message with regexp?

This came up in a LAVA thread, and I'm not sure if there's a bug here or not. When using Search and Replace string, and using a regular expression of [(G[b|i])], LabVIEW throws error -4622, "There is an unmatched parenthesis in a regular expression."  There are obviously no unmatched parenthesis in that expression, so it seems to me that the wrong error is being thrown. I'm just not sure if that's a syntactically valid regexp. The problem seems to be with nesting [ ]'s inside ( )'s inside [ ]'s. I've tried a couple of regexp resources on the Web, and one suggests it's valid, while the other seems to think it isn't.

Message Edited by eaolson on 03-13-2007 10:33 AM

Download All
0 Kudos
Message 1 of 7
(3,346 Views)
I think your regexp is invalid.

In regexps, brackets are not the same as parentheses.  Parens are for grouping, while brackets are for matching one of a class of characters.  Brackets can not be nested.

Your expression "[(G[bi])]", therefore parses as a character class which matches '(', 'G', '[', 'b', or 'i' followed by an unmatched paren, and an unmatched bracket.
0 Kudos
Message 2 of 7
(3,340 Views)
Hi,
what string exactly you want to replace? I think the round braces are not right in this case, since they mark partial matches which are given back by "match regular expression". But you don't want to extract parts of the string, you want to replace them (or delete, with empty <replace string>). So if you leave the outer [( ... )] then your RegEx means all strings with either "Gb" or "Gi".
What are the possible Names of your Standard Unit?
Greets, dave
Greets, Dave
0 Kudos
Message 3 of 7
(3,336 Views)

adambrewster wrote: I think your regexp is invalid. In regexps, brackets are not the same as parentheses.  Parens are for grouping, while brackets are for matching one of a class of characters.  Brackets can not be nested.
If the regexp is replaced with [G[bi]], there is no error, so it's not a matter of nested brackets. I couldn't find anything on the PCRE man page that forbids nested brackets specifically, but it makes sense.

Your expression "[(G[bi])]", therefore parses as a character class which matches '(', 'G', '[', 'b', or 'i' followed by an unmatched paren, and an unmatched bracket.

I don't believe that's the case. Replace the regexp with [(Gbi)], and the error goes away. So it's not a matter of the '(' being literal, and then encountering a ')' without a matching '('.

daveTW wrote: what string exactly you want to replace? I think the round braces are not right in this case, since they mark partial matches which are given back by "match regular expression". But you don't want to extract parts of the string, you want to replace them (or delete, with empty <replace string>). So if you leave the outer [( ... )] then your RegEx means all strings with either "Gb" or "Gi".

It's not my regular expression. A poster at LAVA was having problems with one of his (a truly frightening one), and this seemed to be the element that was causing the problem. I'm pretty sure that the originator of the regexp meant to use G(b|i), which seems like a complicated way of matching "Gb" or "Gi", if you ask me.
0 Kudos
Message 4 of 7
(3,328 Views)
From the doc you linked:

Part of a pattern that is in square brackets is called a "character
class". In a character class the only metacharacters are:

\ general escape character
^ negate the class, but only if the first character
- indicates character range
[ POSIX character class (only if followed by POSIX
syntax)
] terminates the character class


inside of a character class, parens are always literal, and [ is a literal except in POSIX classes, such as [:alpha:].

In your second example, "[(Gbi)]" parsed as a character class which matches '(', 'G', 'b', 'i', or ')'.

If you still don't believe me, try this one: "([[)])". If parens and brackets inside brackets aren't literal, than it will fail.
0 Kudos
Message 5 of 7
(3,322 Views)
And "[G[bi]]" is a valid expression that matches 'G]', '[]','b]', or 'i]'.

The first [ starts a character class.
The second [ is a character literal.
The first ] terminates the character class.
The second ] is a character litral.

Adam


0 Kudos
Message 6 of 7
(3,314 Views)


@adambrewster wrote:
From the doc you linked:

Part of a pattern that is in square brackets is called a "character
class". In a character class the only metacharacters are:

\ general escape character
^ negate the class, but only if the first character
- indicates character range
[ POSIX character class (only if followed by POSIX
syntax)
] terminates the character class


inside of a character class, parens are always literal, and [ is a literal except in POSIX classes, such as [:alpha:].

In your second example, "[(Gbi)]" parsed as a character class which matches '(', 'G', 'b', 'i', or ')'.

If you still don't believe me, try this one: "([[)])". If parens and brackets inside brackets aren't literal, than it will fail.



Ah, you're right. So the inner [ ]'s weren't nested inside the outer [ ]'s at all. The first '[' was a literal character and the first ']' closed the initial '['. The terminating ']' doesn't create an error because it's not a metacharacter when it's not closing an opening bracket.
0 Kudos
Message 7 of 7
(3,311 Views)