我想匹配以下模式的子字符串:Java和正则表达式,得到它匹配
[0-9]*-[0-9]*-[BL]
和模式应用到这个字符串:
123-456-L-234
这应该成为
123-456-L.
这是我的代码:
HelperRegex{
..
final static Pattern KEY = Pattern.compile("\\d*-\\d*-[BL]");
public static String matchKey(String key) {
return KEY.matcher(key).toMatchResult().group(0);
}
的Junit:
@Test
public final void testMatchKey() {
Assert.assertEquals("453-04430-B", HelperRegex.matchKey("453-04430-B-1"));
}
是有没有找到匹配的异常。
我已经证明了我的正则表达式与“正则表达式教练”,似乎没有损坏,并匹配所有的TestString
我只是喜欢看正则表达式教练因为它是一个Lisp应用程序。 – 2013-03-13 19:31:41