1

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.

2
  • How is the code saying no matches were found? Show the unexpected behavior or output. Commented Jun 29, 2012 at 23:41
  • I get a No Match Found error, being thrown by match.group(). Commented Jun 29, 2012 at 23:42

2 Answers 2

2

Almost there, you just need:

Matcher match = pattern.matcher("EN(  0)= 0.000000E+00");
match.find(); // <-- missing
String result = match.group();
Sign up to request clarification or add additional context in comments.

Comments

1

- 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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.