Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

A question about regular expressions

Thread view: 
Covington Bradshaw - 23 Dec 2006 19:45 GMT
Hi every one,

    I have a basic regular expressions question.
It goes like this: How do I extract a sequence of
@-delimited characters from a text?

Example: @{1}.*?@{1}
Will give:
123456789abcdefhghij
when applied to the following text:
@123456789abcdefghij@987654321stuvwxyz@

However I am trying to build a regular expression
for a more complex case where the double @@, which
is the escape character for @, can also be handled.
Consequently I need to know how to extract

123456789@@abcdefhghij

from

@123456789@@abcdefghij@987654321@@stuvwxyz@

using regular expressions.

Note the expression @([^@]|((@@)*))@ was suggested in a
previous reply but this one returns a false positive
on the following text:

@123456789@@abc

by returning the value:
@123456789@

Anyone has a clue?

Thanks
Chris Smith - 23 Dec 2006 21:22 GMT
> Hi every one,
>
>     I have a basic regular expressions question.

Yes.  Can you do us a favor and respond in existing threads, instead of
starting new ones all the time?  Do you know how to reply to an existing
message?

> Note the expression @([^@]|((@@)*))@ was suggested in a
> previous reply but this one returns a false positive
> on the following text:
>
> @123456789@@abc

Okay.  Then try (^|[^@])@([^@]|((@@)*))@($|[^@])

This time, your result will be in group #2.

Signature

Chris Smith

Covington Bradshaw - 23 Dec 2006 23:26 GMT
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(^|[^@])@([^@]|((@@)*))@($|[^@])", Pattern.DOTALL);
        String b = "@123456789@@abcdefghij@987654321@@stuvwxyz@";
        Matcher matcher = pattern.matcher(b);
        System.out.println("find [" +matcher.find() + "]");
        System.out.println("group [" + matcher.group() + "]");
        System.out.println("start [" + matcher.start() + "]");
        System.out.println("end [" + matcher.end() + "]");
    }

This code returns 9@@a

I am expecting @123456789@@abcdefghij@

Am I missing something?

=======================

> Okay.  Then try (^|[^@])@([^@]|((@@)*))@($|[^@])
>
> This time, your result will be in group #2.
Chris Smith - 24 Dec 2006 15:22 GMT
> This code returns 9@@a
>
> I am expecting @123456789@@abcdefghij@
>
> Am I missing something?

Nah.  I just screwed up, and didn't realize it.  This one is actually
tested:

   (^|[^@])@(([^@]|(@@))+)@($|[^@])

Again, answer in group #2.

Signature

Chris Smith



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.