-1

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

16
  • what output you expect to get? Commented May 7, 2017 at 20:26
  • what did you mean by but somehow it only detects the first part of the regex not the last part ? Commented May 7, 2017 at 20:27
  • I expect to get the whole string. Well it only gives me back -2√3254 not the entire string, which is what I expect. @YCF_L Commented May 7, 2017 at 20:29
  • is that all your code? what is the inputs? Commented May 7, 2017 at 20:30
  • 1
    did you mean String m = "-2√3254i/18.5"; String regex = "-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i+\\/+\\d+(\\.\\d*)?"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(m); if (matcher.find()) { System.out.println(matcher.group()); } ? Commented May 7, 2017 at 20:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.