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 / September 2006

Tip: Looking for answers? Try searching our database.

String split regex question

Thread view: 
Adam Lipscombe - 12 Sep 2006 11:06 GMT
Folks

I need to split a String that is separated by "{".

e.g.:
"3835220{AP102            {4257570000429831{CLAIMANT B             {08/09/2005{Books and Periodicals
   {U{40 {WH SMITH                 CHESTER      {   5994{      30.94{        0.94{826{GB"

If I use String.split("{") I get a PatternSyntaxException with a "illegal repetition" message.

I have tried escaping the "{" String to "\\{" and "\\\\{" but the results are the same.

I know "{" is a a meta-char, but sadly I have never really understood how the meta-char escape mech
works in the Java regex syntax....

How does one make the above work?

TIA - Adam
GenxLogic - 12 Sep 2006 11:20 GMT
Use String.split("[{]"); it will work out.
Thanks,
Deepak Kumar
> Folks
>
[quoted text clipped - 14 lines]
>
> TIA - Adam
hiwa - 12 Sep 2006 11:28 GMT
GenxLogic のメッセージ:

> Use String.split("[{]"); it will work out.
> Thanks,
[quoted text clipped - 17 lines]
> >
> > TIA - Adam
Or,
split("\\{")
because { is a special character for regex engine.
Gordon Beaton - 12 Sep 2006 11:22 GMT
> If I use String.split("{") I get a PatternSyntaxException with a
> "illegal repetition" message.

Why don't you use a StringTokenizer for this, which takes a list of
delimiters (not a regex).

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

alfamaster7 - 12 Sep 2006 13:45 GMT
maybe thats what u need..

public final static String[] split( String str, char separatorChar ) {
     if ( str == null ) {
        return null;
     }
     int       len    = str.length();
     if ( len == 0 ) {
        return null;
     }
     Vector    list   = new Vector();
     int       i      = 0;
     int       start  = 0;
     boolean   match  = false;
     while ( i < len ) {
        if ( str.charAt( i ) == separatorChar ) {
           if ( match ) {
              list.addElement( str.substring( start, i ).trim() );
              match = false;
           }
           start = ++i;
           continue;
        }
        match = true;
        i++;
     }
     if ( match ) {
        list.addElement( str.substring( start, i ).trim() );
     }
     String[]  arr    = new String[list.size()];
     list.copyInto( arr );
     return arr;
  }


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.