MattS
MattS

Reputation: 717

Having trouble with java regex

I'm new to regex in Java and am having trouble getting it to work properly. The following code is saying there were no matches found.

pattern = Pattern.compile("EN\\( [ -][0-5]\\)= \\d+.?\\d*E[+-]\\d{2}");
match = pattern.matcher("EN(  0)= 0.000000E+00");
String result = match.group();

As far as I can tell, this should be working. I've been using the Oracle java tutorial on regular expressions to guide me. Any and all help is appreciated.

Upvotes: 1

Views: 62

Answers (2)

Pshemo
Pshemo

Reputation: 124215

- in [ -] is special character so you must use [ \\-]

Pattern pattern = Pattern.compile("EN\\( [ \\-][0-5]\\)= \\d+.?\\d*E[+\\-]\\d{2}");
Matcher match = pattern.matcher("EN(  0)= 0.000000E+00");
if (match.find())
    System.out.println(match.group());

Upvotes: 1

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

Almost there, you just need:

Matcher match = pattern.matcher("EN(  0)= 0.000000E+00");
match.find(); // <-- missing
String result = match.group();

Upvotes: 2

Related Questions