Brian,

Yuriy's suggestion will get your code to compile... But, your regular 
expression won't match the text you're looking for.

You need to get rid of the square braces otherwise you'll only match a first 
directory with a name of "\" or "w"

I suggest you try
   RE exp = new RE ("^/(\\w+)/.*");

this will match one or more word characters in-between the first two slashes.

Ed.


On Saturday 15 December 2001 02:08 pm, Yuriy Zubarev wrote:
> Brian,
>
> You need to escape back slash in Java: type \\w intstead of "\w"
>
> Best of luck,
> Yuriy Zubarev
>
> ----- Original Message -----
> From: "Brian Elliott" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, December 15, 2001 6:37 AM
> Subject: Simple example needed
>
> > I am very familiar with Perl regular expression and have used
> > org.apache.regexp.RE package for a little while but only for simple
> > matching. How do I match a word in a regular expression? According to
> > the RE javadocs:
> >
> > \w                   Matches a "word" character (alphanumeric plus "_")
> >
> > So could someone give me some example Java code? I am analyzing a file
> > path and want to grab the first directory name (ex. "/dir1/dir2/dir3").
> > Like:
> >
> >         String path = "/dir1/dir2/dir3";
> >         RE exp = new RE ("^/([\w])/.*");
> >         boolean matched = exp.match(path);
> >         String insideParens = exp .getParen(1);
> >
> > The compiler complains about \w being invalid. Do I have to put the \w
> > inside of colons or something? Or not include the braces?
> >
> > Also, I know I need to apply the pattern to my path String but do I have
> > to use the match method or are there other ways of doing it?
> >
> > Any help would be appreciated.
> >
> > Brian
> >
> > --
> > To unsubscribe, e-mail:
>
> <mailto:[EMAIL PROTECTED]>
>
> > For additional commands, e-mail:
>
> <mailto:[EMAIL PROTECTED]>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>
>
> --
> To unsubscribe, e-mail:  
> <mailto:[EMAIL PROTECTED]> For additional
> commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to