gpt4 book ai didi

java - 如何更改 java 中的正则表达式搜索以忽略大小写

转载 作者:行者123 更新时间:2023-12-01 07:14:07 25 4
gpt4 key购买 nike

如何更改以下代码,使其不关心大小写?

public static String tagValue(String inHTML, String tag)
throws DataNotFoundException {
String value = null;
Matcher m = null;

int count = 0;
try {

String searchFor = "<" + tag + ">(.*?)</" + tag + ">";

Pattern pattern = Pattern.compile(searchFor);

m = pattern.matcher(inHTML);


while (m.find()) {
count++;


return inHTML.substring(m.start(), m.end());
// System.out.println(inHTML.substring(m.start(), m.end()));
}
} catch (Exception e) {
throw new DataNotFoundException("Can't Find " + tag + "Tag.");
}

if (count == 0) {
throw new DataNotFoundException("Can't Find " + tag + "Tag.");

}

return inHTML.substring(m.start(), m.end());

}

最佳答案

Pattern.CASE_INSENSITIVE标记为Pattern.compile :

String searchFor = "<" + tag + ">(.*?)</" + tag + ">";
Pattern pattern = Pattern.compile(searchFor, Pattern.CASE_INSENSITIVE);
m = pattern.matcher(inHTML);

(哦,考虑 parsing XML/HTML instead of using a regular expression to match a nonregular language 。)

关于java - 如何更改 java 中的正则表达式搜索以忽略大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7323988/

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