I am trying to check if a string matches the pattern of a complex number with the format x+yi
or x-yi
.
My regex for this purpose is ^\d+(\.\d+)?[-\+]\d+(\.\d+)?i
and it works just fine on regexr (regexr.com/572bb).
Here is my minimal example in java:
public class ExampleClass {
public static void main(){
String comp = "3+5i";
System.out.println(comp.matches("^\\d+(\\.\\d+)?[-\\+]\\d+(\\.\\d+)?i"));
}
It throws the above exception. Here it is in full:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
The "+" it's complaining about is this one: [-\\+]
but it doesn't seem like it's dangling since I escaped it. I am a beginner, so I would appreciate any help I can get. Thank you!