【发布时间】:2011-11-15 11:07:49
【问题描述】:
我正在尝试使用我提供的表达式获取我的匹配器能够找到的字符串。像这样的..
if(matcher.find())
System.out.println("Matched string is: " + ?);
什么是合适的代码?根据Oracle
matcher.group();
方法只返回与
相同的提供的输入matcher.group(0);
提前谢谢..
编辑:
示例如下:
private static String fileExtensionPattern = ".*<input type=\"hidden\" name=\".*\" value=\".*\" />.*";
private static Matcher fileXtensionMatcher;
private static String input = text "<html><body><table width="96"><tr><td><img src="file:/test" /><input type="hidden" name="docExt" value=".doc" />Employee Trv Log 2011 Training Trip.doc</td></tr></table></body></html>"
private static void findFileExtension() {
System.out.println("** Searching for file extension **");
System.out.println("Looking for pattern: " + fileExtensionPattern);
fileXtensionMatcher = fileXtensionExp.matcher(input);
if(fileXtensionMatcher.find()) {
//the extension expression is contained in the string
System.out.println("Extension expression found.");
System.out.println(fileXtensionMatcher.group());
}
}
得到的结果是:
text "<html><body><table width="96"><tr><td><img src="file:/test" /><input type="hidden" name="docExt" value=".doc" />Employee Trv Log 2011 Training Trip.doc</td></tr></table></body></html>"
【问题讨论】:
-
你试过了吗?如果您这样做了,您就会知道
matcher.group()将完全满足您的需求。 -
@Marcelo 请阅读对最后一个答案的评论
-
在看到你的源代码后添加了一个答案。