Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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
复杂字符之间的Java正则表达式文本_Java_Regex_String_Data Extraction - Fatal编程技术网

复杂字符之间的Java正则表达式文本

复杂字符之间的Java正则表达式文本,java,regex,string,data-extraction,Java,Regex,String,Data Extraction,我试图在regex的帮助下从字符串中提取文本,但我以前没有太多使用过它,我也无法确定Pattern.compile的格式 我想从以下字符串中减去重量(9盎司): <li><b>Shipping Weight:</b> 9 ounces (<a href="http://www.amazon.com/gp/help/seller/shipping.html?ie=UTF8&amp;asin=0982817509&amp;seller=ATV

我试图在regex的帮助下从字符串中提取文本,但我以前没有太多使用过它,我也无法确定Pattern.compile的格式

我想从以下字符串中减去重量(9盎司):

<li><b>Shipping Weight:</b> 9 ounces (<a href="http://www.amazon.com/gp/help/seller/shipping.html?ie=UTF8&amp;asin=0982817509&amp;seller=ATVPDKIKX0DER">View shipping rates and policies</a>)</li>
装运重量:9盎司()
print(“实际重量:+link.outerHtml());
Pattern p=Pattern.compile(“权重:\\\(.*?\ \)\(”);
Matcher m=p.Matcher(link.outerHtml());
m、 查找();
系统输出println(m.group(1));
我的Pattern.compile格式应该是什么?我正在尝试在“Weight:”和“(”之间切换


任何帮助都将是惊人的!我已经搜索了一段时间,但我找不到一个好地方来解释格式。

您似乎没有逃过最后的
,所以这将是一个问题(我想,我不使用Java-考虑到正则表达式中使用括号来表示组)。我还添加了
\s
,这意味着您不必修剪结果

Pattern.compile("Weight:</b>\s+(.*?)\s+\(");
Pattern.compile(“权重:\s+(.*)\s+\(”;

您甚至不需要分组。在这种情况下,“回头看”是有效的:

Pattern p = Pattern.compile("(?<=Weight:</b> )[^(]*");
Pattern p=Pattern.compile((?)作为替代:

Pattern.compile("\d*\sounces");

这是一个危险的链接…谁知道那
链接的文本是什么?我不认为它是什么了-我编辑了它。它应该工作得很好。很好,很容易阅读。只需添加“权重”“在其他代码中使用时,在您的组前面进行匹配。无需为常量正则表达式您要替换或匹配感谢所有的帮助!看起来我把事情复杂化了,我只使用了:Pattern p=Pattern.compile(“Weight:(.*)\)”);这似乎是在耍花招!我无法使用\s,但你是对的,我没有逃过最后一个括号。
Pattern.compile("\d*\sounces");