0

I recently saw how to solve a problem of mine using java regex and the split method, so i wanted to learn regex I watched a few 20-30 min videos, and I thought I understood it, but in reality, those videos are only covering the basics I assume. Because there seems to be an order that expressions go, in order to split. for instance, this code splits the coefficients up but based off the knowledge from those videos this would actually split the x and the ^ char and ANY occurrence of a digit as well as any occurrence of a "+" But instead it splits the function so just the coefficients are left, Why?

code:

String polynomial = "-2x^2+3x^1+6"

String[] parts = polynomial.split("x\\^\\d+\\+?")
for (i = 0; i < part.length; i++){
System.out.println(parts[i])
}

I tried to write a split function that splits any num after the ^ char but before the + symbol of each "part" by doing \\d+\\x\\+ which just gave me an error.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
userr554
  • 57
  • 5
  • 1
    The error is because you wrote `\\x` where you might have just meant `x`. But fixing that won't give you a solution. The point about `split` is that it actually removes the portion of the text that matches the regular expression; then puts whatever pieces are left behind into an array. I don't think that's what you want to do. – Dawood ibn Kareem Apr 08 '23 at 23:11
  • You could try a double split foirst on + and then on ^ but it would be better to explore [capturing groups](https://stackoverflow.com/questions/17969436/java-regex-capturing-groups). `x\\^(\\d+)\\+` – LMC Apr 08 '23 at 23:15
  • 2
    "I recently saw how to solve a problem of mine using java regex and the split method" What problem? What you trying to _actually_ do here, irrespective of whether you use regexp or not? – Mike 'Pomax' Kamermans Apr 08 '23 at 23:32
  • Re. Mike The problem was essentially just the same problem im facing but with the coefficients instead of anything after the ^ but I found the coefficients answer online, so I didn't know what was going on inside the parentheses. Now I want to learn how to use split and Regex in order to parse anything after ^ but before the + into its own array – userr554 Apr 08 '23 at 23:37
  • 2
    What is the desired result after the split? – The fourth bird Apr 09 '23 at 16:19
  • Wouldn't splitting into its parts be the goal? eg `-2x^2+3x^1+6` split into the coefficients and numerals `[-2, 2, 3, 1, 6]`? If so, try splitting on `"[^\\d-]+"` – Bohemian Apr 14 '23 at 03:59
  • 1
    @Mscarb if you have a solution, remember to post an answer, or delete your post if you think no one else would benefit from learning what you leanred. Don't update the post to say "there is an answer" and then not show that answer (and _definitely_ don't put the answer in the post itself =) – Mike 'Pomax' Kamermans Apr 14 '23 at 04:13

1 Answers1

0

There Are several answers to my question one would be to split the function by the + and therefore have the args split up instead of exclusively the coefficients i.e polynomial.split("+")

Alternatively: this "[^\\d-]+" will also work if you are looking for an answer utilizing java regex than this might be what your looking for -- Answer provided by: Bohemian ♦

userr554
  • 57
  • 5