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.

regular expressions

Thread view: 
Covington Bradshaw - 17 Dec 2006 20:00 GMT
How to extract 123456789@@abcdefhghij from

@123456789@@abcdefghij@987654321@@stuvwxyz@

using regular expressions

Thanks
Andrew Thompson - 17 Dec 2006 20:19 GMT
> How to extract 123456789@@abcdefhghij from
>
> @123456789@@abcdefghij@987654321@@stuvwxyz@
>
> using regular expressions

1) Please add question marks to questions.
2) Don't forget to quote your budget for the answer,
when treating this discussion forum as a help-desk.

Andrew T.
Chris Smith - 17 Dec 2006 20:30 GMT
> How to extract 123456789@@abcdefhghij from
>
> @123456789@@abcdefghij@987654321@@stuvwxyz@
>
> using regular expressions

Unfortunately, you haven't formulated you question well enough to be
precise.  Presumably, you have some idea what you'd want for other
inputs, as well.  What would you want for other inputs?  That will help
people give the answer you want.

If you don't care about the result for other inputs, then:

   public void sillyMethod(String input)
   {
       return "123456789@@abcdefhghij";
   }

is the simplest method that does what you asked, and you don't even need
regular expressions.

Signature

Chris Smith

Flo 'Irian' Schaetz - 17 Dec 2006 20:38 GMT
And thus, Covington Bradshaw spoke...

> How to extract 123456789@@abcdefhghij from
>
> @123456789@@abcdefghij@987654321@@stuvwxyz@
>
> using regular expressions

Why regular expressions?

String myString = "@123456789@@abcdefghij@987654321@@stuvwxyz@";

int first = myString.indexOf("@")+1;
int third = myString.indexOf("@", myString.indexOf("@", first)+1);
       
String n = myString.substring(first, third);

Of course, that's just a creative guess...

Flo
John Ersatznom - 17 Dec 2006 21:03 GMT
> And thus, Covington Bradshaw spoke...
>
[quoted text clipped - 10 lines]
> int first = myString.indexOf("@")+1;
> int third = myString.indexOf("@", myString.indexOf("@", first)+1);

Shouldn't that be

int third = myString.indexOf("@", myString.indexOf("@", first)+2);

?

I assume the idea is to parse @foo@@bar@baz@@quux@quuux... into the
"foo@@bar" records, so you actually want something like

public static List<String> getRecords (String input)
        throws FormatException {
    int index = 0;
    int len = input.length() - 1;
    List<String> result = new LinkedList<String>();
    while (index < len) {
        int nextStart = input.indexOf('@');
        if (nextStart == -1) throw new FormatException();
        int nextMid = input.indexOf("@@", nextStart + 1);
        if (nextMid == -1) throw new FormatException();
        index = input.indexOf("@", nextMid + 2);
        if (index == -1) throw new FormatException();
        result.add(input.substring(nextStart + 1, index));
    }
    return result;
}

Result:
foo@@bar, baz@@quux, ...

Or maybe you want key/value pairs?

public static Map<String, String> getRecords (String input)
        throws FormatException {
    int index = 0;
    int len = input.length() - 1
    Map<String, String> result = new HashMap<String, String>();
    while (index < len) {
        int nextStart = input.indexOf('@');
        if (nextStart == -1) throw new FormatException();
        int nextMid = input.indexOf("@@", nextStart + 1);
        if (nextMid == -1) throw new FormatException();
        index = input.indexOf("@", nextMid + 2);
        if (index == -1) throw new FormatException();
        String key = input.substring(nextStart + 1, nextMid);
        String value = input.substring(nextMid + 2, index);
        if (key.length() == 0 || value.length() == 0)
            throw new FormatException();
        // Optional if duplicate keys are bad.
        if (result.containsKey(key))
            throw new FormatException();
        // End optional
        result.put(key, value);
    }
    return result;
}

Result: foo -> bar; baz -> quux; ...

(Both of the above should throw the exception of your choice if the
input isn't empty and its format isn't exactly as given above: @
followed by however-many occurrences of foo@@bar@. The latter disallows
empty strings, e.g. @foo@@@baz@@quux@.)
Flo 'Irian' Schaetz - 17 Dec 2006 21:21 GMT
And thus, John Ersatznom spoke...

> Shouldn't that be
>
> int third = myString.indexOf("@", myString.indexOf("@", first)+2);

My fault, I missed the 2nd @ in the string. I thought it was
@something@anothersomething@.

Flo


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.