472,364 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

Can't do str.split("|") ?

15
Hi,

I'm trying to read a file with data seperated by "|" character.
Ex:
3578.27|2001|Road Bikes|Metchosin|Canada
3399.99|2001|Mountain Bikes|Pantin|France
3399.99|2001|Mountain Bikes|Lebanon|United States

After reading the first line, I'm trying to split the string into an array by doing
Expand|Select|Wrap|Line Numbers
  1. String[] x = str.split("|");
  2.  
However java see "|" as an alternation rather than a regex delimiter for my string.

And I cannot change the data file because it has over 3000 lines.

Regards,
Gordon
Feb 18 '08 #1
7 5152
JosAH
11,448 Expert 8TB
The '|' character is special to the regular expression compiler so you should escape
it with a '\' (backslash), but then again the backslash is also special to the Java
compiler (javac) so you should escape it twice; use "\\|"

kind regards,

Jos
Feb 18 '08 #2
hirak1984
316 100+
If the trailing fields are optional, you need to write:
Expand|Select|Wrap|Line Numbers
  1. line.split("\\|",-1);
demo:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class Splitsville {
  4.     public static void main(String[] args) {
  5.         String[] data = {
  6.             "a|b|c|d|e",
  7.             "||||",
  8.             "||c||"
  9.         };
  10.         for(String line : data) {
  11.             String[] tokens = line.split("\\|");
  12.             System.out.println("split: " + Arrays.toString(tokens));
  13.             String[] tokens2 = line.split("\\|",-1);
  14.             System.out.println("split  with -1: " + Arrays.toString(tokens2));
  15.         }
  16.     }
  17. }
Feb 18 '08 #3
r035198x
13,262 8TB
Hi,

I'm trying to read a file with data seperated by "|" character.
Ex:
3578.27|2001|Road Bikes|Metchosin|Canada
3399.99|2001|Mountain Bikes|Pantin|France
3399.99|2001|Mountain Bikes|Lebanon|United States

After reading the first line, I'm trying to split the string into an array by doing
Expand|Select|Wrap|Line Numbers
  1. String[] x = str.split("|");
  2.  
However java see "|" as an alternation rather than a regex delimiter for my string.

And I cannot change the data file because it has over 3000 lines.

Regards,
Gordon
Escape the special character using \\
i.e split("\\|");
Feb 18 '08 #4
r035198x
13,262 8TB
first of all, this is not a solution. It is a suggestion rather.

Expand|Select|Wrap|Line Numbers
  1. String str = "3578.27|2001|Road Bikes|Metchosin|Canada";
  2.         String str1 = str.replace("|", ",");
  3.         String[] x = str1.split("'");
  4.         int i=0;
  5.         while (i<x.length){
  6.         System.out.println(x[i++]);
  7.         }
  8.  
What if there are words with ' in them?
Feb 18 '08 #5
hirak1984
316 100+
actually this works:
Expand|Select|Wrap|Line Numbers
  1. Str.split("\\|");
  2.  
Feb 18 '08 #6
r035198x
13,262 8TB
actually this works:
Expand|Select|Wrap|Line Numbers
  1. Str.split("\\|");
  2.  
Yes. That's the third time that that is being said. The explanation is in the first reply.
Feb 18 '08 #7
spoken
15
Thank you all for your replies.

Yes,
str.split("\\|");
works perfectly.

Thanks again.. :D
Feb 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Joerg Schuster | last post by:
Hello, The program given below returns the lines: a ab Is there a way to use python regular expressions such that the program would return the following lines?
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
35
by: les_ander | last post by:
Hi, I know that i can do readline() from a file object. However, how can I read till a specific seperator? for exmple, if my files are name profession id #
2
by: VM | last post by:
How can I separate a string that looks like this: "John|Conner|34323|T3-1" so I can place John in one variable, Conner in another variable, etc...? I know you can do it by finding the...
6
by: NuBBeR | last post by:
I need to know how to grab a substring between "{" and "}". The full text has many instances of these brackets and i need to find the string...
4
by: michael.bollhoefer | last post by:
I am trying to split a string into two different variables. The string is pulled from an XML file from a WEBDAV program. The string will be in this form "John Doe" <john.doe@webserver.com> ...
3
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print...
2
by: Alexander Eisenhuth | last post by:
.... as you can find in os.py at line 1 ? Regards Alexander
2
by: elgin | last post by:
I have a split Access 2003 database. I have signed the database with a Code Signing Certificate from Small Business Server. This works fine and users can have Access macro security on high or...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.