gpt4 book ai didi

Java:从输入中获取匹配的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:56 24 4
gpt4 key购买 nike

我正在尝试获取我的匹配器能够使用我提供的表达式找到的字符串。像这样的..

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=&quot;file:/test&quot; /><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=&quot;file:/test&quot;  /><input type="hidden" name="docExt" value=".doc" />Employee Trv Log 2011 Training Trip.doc</td></tr></table></body></html>"

最佳答案

为什么您认为 group() 会返回输入?

根据 the JavaDoc :

Returns the input subsequence matched by the previous match.

换句话说:它返回匹配的输入的那部分

关于Java:从输入中获取匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com