Page 1 of 1
Reg exp's and ?, *, and +
Posted: Thu Nov 29, 2001 9:20 pm
by Mark Schnitzius
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?
Re: Reg exp's and ?, *, and +
Posted: Fri Nov 30, 2001 8:03 am
by Jens Hollmann
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.
Re: Reg exp's and ?, *, and +
Posted: Fri Nov 30, 2001 8:28 pm
by Roy Beatty
Try:
\[?.*\]
or
\[\{0,1\}.*\]
Putting the X back in Xmas,
Roy
Re: Reg exp's and ?, *, and +
Posted: Fri Nov 30, 2001 10:02 pm
by Mark Schnitzius
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.
Re: Reg exp's and ?, *, and +
Posted: Sat Dec 01, 2001 7:50 am
by Ed Orchard
I have a feeling that I need another example. But the reg exp \[[^]]*\] satisfies the above conditions.
Ed
Re: Reg exp's and ?, *, and +
Posted: Wed Dec 05, 2001 4:14 pm
by Mark Schnitzius
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".
Re: Reg exp's and ?, *, and +
Posted: Wed Dec 05, 2001 7:04 pm
by Andreas
try \(\[\]\)? to match [] or nothing
Grouping is done by \(\), not [] which is for character classes
Re: Reg exp's and ?, *, and +
Posted: Fri Dec 07, 2001 5:45 pm
by Doug Wong
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.