简体   繁体   English

有java正则表达式的麻烦

[英]Having trouble with java regex

I'm new to regex in Java and am having trouble getting it to work properly. 我是Java的正则表达式的新手,并且无法让它正常工作。 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. 我一直在使用正则表达式Oracle java教程来指导我。 Any and all help is appreciated. 任何和所有的帮助表示赞赏。

Almost there, you just need: 几乎在那里,你只需要:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM