hey there,
I am trying to test strings using regex to make sure that the string may represent a monomial.

that is, it must be 123123213x^213123.

I am having difficulties to find any helpful information on how to test for ^.

I am trying this: [+|-][0-9]*[.][0-9]*[x\^]? but I get a complaint that ^ is not a valid escape sequence (I am using eclipse).

in case you wonder what that [.] is, it is to make sure doubles are matched for.

anyone any idea please.

thank you.

6 Years
Discussion Span
Last Post by bibiki

\^ isn't an escape sequence, I think what you are looking for is a char literal? i.e. '^'


Hey Ricky116,
this is what I got right now:


however, this has a loop because it allows to input 123^3 when a user should actually input 123x^3. I know I can add another if and add the x if no x is there, but can you make that regular expression any better man? It is supposed to test strings to see if they can be monomials.


Hi, Try this..and let me know ..whether u are looking for same

public class RegExp {
   public static void main(String[]args)
        String test = "123123213x^213123.";
        System.out.println("Testing " + test);
        boolean b = test.matches("^[0-9]*[x][\\^][0-9]*[\\.]");

Edited by msvinaykumar: used QUOTE which is not required


with minor modifications. pretty much works. thank you man.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.