aspose file tools*
The moose likes Java in General and the fly likes String split - Regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String split - Regular expression" Watch "String split - Regular expression" New topic
Author

String split - Regular expression

Abi Raj
Greenhorn

Joined: Jan 28, 2005
Posts: 28
I want to split a string into array of strings. the delimiter is ,.

for example the string when splited
"\\ma\\", "g,a", "\"AL","\"wA\"", in, "or"

should return an array of the following strings.

\\ma\\
g,a
\"AL
\"WA\"
in
or

Can I use a regular expression and if so how?

Thanks in Advance.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29275
    
140

Abi,
You are correct - Java 1.4 and higher have a split method that takes a regular expression:
String[] result = string.split(regExp)

The delimiter is not just a comma in your example though. It looks like it is a comma not surrounded by quotes or a comma followed by a space. I can't tell which one from the example.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Your string has to be adjusted for compiling and the closest I get is with the following code



The problem is that the sign \ is an escape karakter in Java.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://www.coderanch.com/t/379936/java/java/Regular-Expression-Parse-CSV


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String split - Regular expression
 
Similar Threads
doubt in split method
regular expression in java question!
Regular Expression
splitting arrays
how to avoid NumberFormatException