473,323 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 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 5304
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.