Thought I knew my RE's, but I can't seem to figure out how to do the following...
How would I match ZERO OR ONE (i.e. using the ? operator) of the left
bracket, followed by zero or more characters, followed by the right bracket?
I'd think it would be
[[\[][.*][\]]]?
but no dice, due in part to brackets within brackets being misunderstood by
textpad. Can anyone figure this out?
Reg exp's and ?, *, and +
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
-
Jens Hollmann
Re: Reg exp's and ?, *, and +
Maybe you should give an example with all (or at least most of) the possible situations where to find something. I don't quite get what you want.
-
Roy Beatty
Re: Reg exp's and ?, *, and +
Try:
\[?.*\]
or
\[\{0,1\}.*\]
Putting the X back in Xmas,
Roy
\[?.*\]
or
\[\{0,1\}.*\]
Putting the X back in Xmas,
Roy
-
Mark Schnitzius
Re: Reg exp's and ?, *, and +
Nope, that would match "[blah"; what I want is to match
[]
[123]
[abc]
but not
abc
[123
123]
This should be doable! I think there may be a textpad bug in that it doesn't seem to allow a literal square bracket inside a square-bracketed expression.
[]
[123]
[abc]
but not
abc
[123
123]
This should be doable! I think there may be a textpad bug in that it doesn't seem to allow a literal square bracket inside a square-bracketed expression.
-
Ed Orchard
Re: Reg exp's and ?, *, and +
I have a feeling that I need another example. But the reg exp \[[^]]*\] satisfies the above conditions.
Ed
Ed
-
Mark Schnitzius
Re: Reg exp's and ?, *, and +
Sorry I wasn't clear... I'm trying to match a Java parameter type, like
you'd find in a method declaration. For instance, the "int" and the
"String[]" parts of this method header:
public void myMethod( int index, String[] list )
There can be zero or one of the bracketed expression following the variable
type. There doesn't seem to be any way to match zero or one of a bracket;
what I'd want is something like
[a-zA-Z_]+[\[.*\]]?
but that doesn't work. Your suggestion of
\[[^]]*\]
matches "one" bracketed expression, not "zero or one".
you'd find in a method declaration. For instance, the "int" and the
"String[]" parts of this method header:
public void myMethod( int index, String[] list )
There can be zero or one of the bracketed expression following the variable
type. There doesn't seem to be any way to match zero or one of a bracket;
what I'd want is something like
[a-zA-Z_]+[\[.*\]]?
but that doesn't work. Your suggestion of
\[[^]]*\]
matches "one" bracketed expression, not "zero or one".
-
Andreas
Re: Reg exp's and ?, *, and +
try \(\[\]\)? to match [] or nothing
Grouping is done by \(\), not [] which is for character classes
Grouping is done by \(\), not [] which is for character classes
-
Doug Wong
Re: Reg exp's and ?, *, and +
I agree with Andreas that his RE should work:
"\(\[\]\)?"
using '\(' and '\)' to form a group of characters. However, the results might not be what you expect. I tried this RE and it simply moved the cursor to the next character position. At first I thought that there is a bug with the '?' operator, but after thinking about it, this seems to be the correct behavior. '?' matches zero or one occurences of the preceeding RE. Zero occurences of any RE is a tricky concept. I've had numerous mistakes like this using the '*' operator.
Mark -
the solution to your problem may be to find the parameter types in two passes; on the first pass find the non-bracketed types and on the second pass find the bracketed types.
"\(\[\]\)?"
using '\(' and '\)' to form a group of characters. However, the results might not be what you expect. I tried this RE and it simply moved the cursor to the next character position. At first I thought that there is a bug with the '?' operator, but after thinking about it, this seems to be the correct behavior. '?' matches zero or one occurences of the preceeding RE. Zero occurences of any RE is a tricky concept. I've had numerous mistakes like this using the '*' operator.
Mark -
the solution to your problem may be to find the parameter types in two passes; on the first pass find the non-bracketed types and on the second pass find the bracketed types.