[vox-tech] regex help - matching literal []

Micah J. Cowan micah at cowan.name
Fri Apr 28 14:46:46 PDT 2006


On Fri, Apr 28, 2006 at 04:19:34PM -0500, Ken Bloom wrote:
> On Friday 28 April 2006 16:05, Micah J. Cowan wrote:
> > On Fri, Apr 28, 2006 at 01:25:41PM -0700, Rod Roark wrote:
> > > On Friday 28 April 2006 01:07 pm, Peter Jay Salzman wrote:
> > > ...
> > >
> > > > However, the compiler complains when I escape the square brackets
> > > > with a backslash.  For some reason \[ doesn't match a literal [.
> > >
> > > Sounds like you need to double up on the backslashes -- once for
> > > the compiler's parsing of string literals, and once for the regex
> > > parser.
> >
> > You could also use "[[].*]".
> 
> Hrm? I don't think so. 
> 
> [bloom at cat-in-the-hat ~]$ bsh
> BeanShell 2.0b4 - by Pat Niemeyer (pat at pat.net)
> bsh % System.out.println("[ioaudsoif]".matches("[[].*]"));
> // Error: // Uncaught Exception: Method Invocation matches : at Line: 
> 1 : in file: <unknown file> : .matches ( "[[].*]" ) 
> 
> Target exception: java.util.regex.PatternSyntaxException: Unclosed 
> character class near index 5
> [[].*]
>      ^
> 
> java.util.regex.PatternSyntaxException: Unclosed character class near 
> index 5
> [[].*]
>      ^

Then it violates POSIX regex syntax. That's a broken response, IMO.

...Checking the Java specs, I see they've "extended" the character class
syntax.

*Sigh*, ah, well... it's a standard method for achieving this in sed,
grep, awk and vi; but for Java it looks like you'll need to
double-escape, as Rod suggested.

-- 
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/


More information about the vox-tech mailing list