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;
}