Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java regex recognize &

Posted on 2009-04-24
18
Medium Priority
?
302 Views
Last Modified: 2012-05-06
I want to pick up the operators in an expression

String[] myArray = formula.split("([\\+-\\&])");

it worked until I added \\& at the end. I also tried & without the \\.
it says illegal character range.

anyone know how to look for the & char in the expression?
0
Comment
Question by:bhomass
  • 5
  • 5
  • 4
  • +2
18 Comments
 
LVL 2

Expert Comment

by:itzy
ID: 24230254
try str.split(Pattern.quote("+-&"));
0
 

Author Comment

by:bhomass
ID: 24230387
sorry. I am using it inside GWT. I can not use Pattern.
0
 
LVL 85

Expert Comment

by:ozo
ID: 24231003
"([+\\-&])"
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24231122
What do you want to match other than '&'
0
 

Author Comment

by:bhomass
ID: 24232718
yes, I want to match also + and -. later I will add *, /, and ^.

to ozo, why is hte the \\ in front of -? I did not know that - is a special char. on the other hand, + and & are both special char, why did you remove \\ from them?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 24232731
'-' is special in a character class, meaning a range. Inside a character class, not of the others is special
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24232734
not of the others=none of the others
0
 
LVL 28

Expert Comment

by:rrz
ID: 24232973
Did you try using ?
  String[] myArray = formula.split("([+,-,&])");
0
 
LVL 28

Expert Comment

by:rrz
ID: 24232986
or  
  String[] myArray = formula.split("([+,-,&,^,*,/])");
0
 

Author Closing Comment

by:bhomass
ID: 31574441
apparently, you are right!
0
 
LVL 28

Expert Comment

by:rrz
ID: 24234147
My solution works as well.
0
 
LVL 85

Expert Comment

by:ozo
ID: 24234338
>  String[] myArray = formula.split("([+,-,&])");
If you want to split on , you only need to include it once
0
 

Author Comment

by:bhomass
ID: 24234473
rrz, I am sure you know how char group works in regex. like ozo says, it is odd to include any char multiple times in the pattern. and I don't even want to split on ",".
0
 
LVL 28

Expert Comment

by:rrz
ID: 24235241
ozo  and  bhomass    
I am not splitting on a comma.   I am using a comma separated list.
Just try it.  It does work.  
0
 
LVL 28

Expert Comment

by:rrz
ID: 24236524
I see now. It does split on a comma. I was wrong.
0
 

Author Comment

by:bhomass
ID: 24236779
it does work for a simple example, but it seems to me to be a hack. there is no mentioning of comma delimiter for a character group in the literature, and there is the side effect of having the comma as a delimiter as well, which I don't want.
0
 
LVL 85

Expert Comment

by:ozo
ID: 24236815
It also does not split on -
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24239416
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question