Java正则表达式代码标题
我需要一个html标签标题上的“Java正则表达式”,但它不起作用。为什么?Java正则表达式代码标题,java,regex,Java,Regex,我需要一个html标签标题上的“Java正则表达式”,但它不起作用。为什么? Pattern.compile("\\<td class=\"codeTitle\">(.*)\\</td>"); Matcher m = p.matcher("<td class="codeTitle">Java RegEx</td>"); Pattern.compile(“\\(.*)\\”; Matcher m=p.Matcher(“Java正则表达式”); 您
Pattern.compile("\\<td class=\"codeTitle\">(.*)\\</td>");
Matcher m = p.matcher("<td class="codeTitle">Java RegEx</td>");
Pattern.compile(“\\(.*)\\”;
Matcher m=p.Matcher(“Java正则表达式”);
您需要调用m.find()
检查是否找到匹配项。如果找到匹配项,您可以使用m.group(1)
访问它们
此外,我认为您忘了转义主题字符串
Pattern.compile("<td class=\"codeTitle\">(.*?)</td>"); //lazy matching is better in matching html tags
Matcher m = p.matcher("<td class=\"codeTitle\">Java RegEx</td>"); // you didn't escape that
if(m.find()){
//do something with m.group(1) which contains "Java Regex"
}
else {
//no matches found
}
Pattern.compile((*?)//惰性匹配在匹配html标记时更好
Matcher m=p.Matcher(“Java正则表达式”);//你没有逃脱
if(m.find()){
//对包含“javaregex”的m.group(1)执行一些操作
}
否则{
//没有找到匹配项
}
您需要调用m.find()
检查是否找到匹配项。如果找到匹配项,您可以使用m.group(1)
访问它们
此外,我认为您忘了转义主题字符串
Pattern.compile("<td class=\"codeTitle\">(.*?)</td>"); //lazy matching is better in matching html tags
Matcher m = p.matcher("<td class=\"codeTitle\">Java RegEx</td>"); // you didn't escape that
if(m.find()){
//do something with m.group(1) which contains "Java Regex"
}
else {
//no matches found
}
Pattern.compile((*?)//惰性匹配在匹配html标记时更好
Matcher m=p.Matcher(“Java正则表达式”);//你没有逃脱
if(m.find()){
//对包含“javaregex”的m.group(1)执行一些操作
}
否则{
//没有找到匹配项
}
什么不起作用?要获取(.*)
的值,需要使用.Pattern p=Pattern.compile(“\\(.*)\\”;Matcher m=p.Matcher(“Java正则表达式”);这个代码工作,但当我使用;Pattern.compile(“\\(.*)\\”;Matcher m=p.Matcher(“Java正则表达式”);不起作用:我要努力弄清楚什么不起作用,为什么不起作用,以及你期望它做什么。什么不起作用?要获取(.*)
的值,需要使用.Pattern p=Pattern.compile(“\\(.*)\\”;Matcher m=p.Matcher(“Java正则表达式”);这个代码工作,但当我使用;Pattern.compile(“\\(.*)\\”;Matcher m=p.Matcher(“Java正则表达式”);不起作用:做一些努力,弄清楚什么不起作用,为什么不起作用,以及你期望它做什么。