• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex problem

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this fragment of the code:
...
String SEP=";\\|,";
String q="2000|3000|1000";
String[] sp=q.split(SEP);
System.out.println(sp.length);

I expected to get a '3' as result. But I am getting '1'.
I wanted to split 'q' on '|'.
What am I doing wrong. I tried :
SEP=";|," and SEP=";\\\\|," with the same result
Tnx,
Haris
 
Haris Karameh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never mind, figured it out:
String SEP=";|\\||,";
[ October 28, 2008: Message edited by: Haris Karameh ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want to split on the | (vertical bar)



or split on the vertical bar, and on the comma, and on the semicolon?
(which don't happen to be in your source string)
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Haris Karameh:
never mind, figured it out:
String SEP=";|\\||,";

[ October 28, 2008: Message edited by: Haris Karameh ]



For multiple single characters, use a character class.
Note that the pipe character | does not have any special meaning inside a character class, so it doesn't need to be quoted with a (pair of) backslash(es) \\
 
Haris Karameh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx Darryl,
that is what I was looking for. I do not use regex frequently .
Your solution is more elegant.
reply
    Bookmark Topic Watch Topic
  • New Topic