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.

Recommended Answers

All 4 Replies

\^ 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:

[+|-]?[0-9]*[.]?[0-9]*[x]?[\\\\\\^]?[0-9]*[.]?[0-9]*

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]*[\\.]");
        System.out.println(b);
    }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.