String split regex question

A

Adam Lipscombe

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
 
G

Gordon Beaton

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
 
H

hiwa

GenxLogic ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
Use String.split("[{]"); it will work out.
Thanks,
Deepak Kumar
Adam said:
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
Or,
split("\\{")
because { is a special character for regex engine.
 
A

alfamaster7

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top