Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex前瞻&;用Java进行后视_Java_Regex_Expression_Lookahead - Fatal编程技术网

Regex前瞻&;用Java进行后视

Regex前瞻&;用Java进行后视,java,regex,expression,lookahead,Java,Regex,Expression,Lookahead,我试图使用Java正则表达式从HTML页面解析数据,但运气不太好。数据是动态的,通常包括零到多个空格、制表符和新行实例。此外,根据点击次数的不同,我正在解析的字符串的结构可能会发生变化。以下是最干净格式的示例: <div class="center">Showing 25 of 2,343,098 (search took 1.245 seconds)</div> HTML不是常规语言,无法使用正则表达式准确解析。当将来标记的格式发生变化时,基于Regex的解决方案可能

我试图使用Java正则表达式从HTML页面解析数据,但运气不太好。数据是动态的,通常包括零到多个空格、制表符和新行实例。此外,根据点击次数的不同,我正在解析的字符串的结构可能会发生变化。以下是最干净格式的示例:

<div class="center">Showing 25 of 2,343,098 (search took 1.245 seconds)</div>

HTML不是常规语言,无法使用正则表达式准确解析。当将来标记的格式发生变化时,基于Regex的解决方案可能会崩溃,但基于解析器的解决方案将更加准确

但是,如果这是一次性工作,则可以使用以下正则表达式:

Showing\s+(?:\d+\s+of\s+)?([\d,.]+)\s+\(search
这些例子表明

"Showing\\s+\\d+\\s+(of\\s+[\\d,.]+\\s+)?\\(search"

“我正在尝试使用Java正则表达式解析HTML页面中的数据”,不再是。另外,你知道
[…]
(…)
之间的区别吗?我没有太多的运气,这就是为什么你应该使用html解析器的原因,所以请诚实地告诉我们你对使用正则表达式解析html的感觉如何?让我重说一遍。“我有一个很长的字符串需要解析”,它包含一堆打开和关闭的carot!即使“of N”不存在,你是否想检测“Showing…(search…)(显示…(搜索…)”对我来说也无关紧要。如果不想,只需删除“?”。谢谢阿马尔!这是一项一次性工作,考虑到“Pshemo的帖子我应该更清楚。最后添加\b以使其更精确。+1
String pattern1 = "Showing [0-9]*\\S"; // not useful
String pattern2 = "[[\\d,+\\.?\\d+]*[\\s*\\n]\\(search took"; //fails
String pattern3 = "(/i)(Showing)(.+?)(\\(search took)"; //fails
String pattern4 = "([\\s\\S]*)\\(search took"; //fails
String pattern5 = "(?s)[\\d].*?(?=\\(search took)"; //close...but fails

Pattern pattern = Pattern.compile(pattern5);
Matcher matcher = pattern.matcher(text); // text = the string I'm parsing
while(matcher.find()) {
    System.out.println(matcher.group(0));
}
Showing\s+(?:\d+\s+of\s+)?([\d,.]+)\s+\(search
"Showing\\s+\\d+\\s+(of\\s+[\\d,.]+\\s+)?\\(search"