-2

I'm trying to define a regex pattern that searches for a caret character, but since ^ is used for negation, I'm not sure how to define the pattern. I'm trying to make the program find a string that is a letter then a caret then a number (as you may have guessed, this is a mathematical term), such as "x^23". This is the line I tried:

String caseFour = "[a-zA-Z]" + "^" + "\\d+";

It's not working. Can anyone help me out?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Shubhang Desai
  • 329
  • 3
  • 17

1 Answers1

0

You need to escape that character also since it is a character of special meaning.

String regex = "[a-zA-Z]\\^\\d+";
hwnd
  • 69,796
  • 4
  • 95
  • 132