i_o
i_o

Reputation: 789

Java Regex not working as desired

I created this regex, but somehow it only detects the first part of the regex not the last part. I would like to know what is going on?

Here's the code:

String m = -2√3254i/18.5
String regex = "-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\/\\d+(\\.\\d*)?"

I have tried many different ways, such as:

-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i+\\/+\\d+(\\.\\d*)?
-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i/\\d+(\\.\\d*)?
-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\(\\/\\d+(\\.\\d*))?

none of them work.

the output is always

-2√3254

Any suggestions,

thank you

Upvotes: -1

Views: 82

Answers (1)

i_o
i_o

Reputation: 789

Okay so my regex is actually composed of many regexes:

String regex = "a regex | another regex | -?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?\\i" 
              + "|another regex | -?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\/\\d+ (\\.\\d*)? 

The problem happens between both regexes shown. The first symbolical regex is picked up by the matcher first, but I really intended for the second symbolical regex to pick up my String m = "-2√32454i/18.5"

It seems the matcher exits matching when one of the boolean conditions is met. All I had to do was rearrange the order of my regexes which make up my regex.

Upvotes: 1

Related Questions