我正在用Java编写一个正则表达式,我试图在字符串中的css“margin:”速记属性中找到下边距,看看它是否为负。可以使用 1、2、3 或 4 个值指定边距属性,以px、em或%结尾,这些值可能为负数和/或以点开头。值之间用一个或多个空格分隔。到目前为止尝试的是这样的正则表达式:
//E.g. style may look like "... margin: 10px 2px" or "... margin: -.10em 1em 2em" etc.
public void findMargin(String style)
{
Pattern compile = Pattern.compile("margin:\\s*(-?\\.?\\d+(?:em|px|%)\\s*){1,4}");
Matcher matcher = compile.matcher(style);
while (matcher.find())
{
.....
}
}
我很难找到提取底边距属性。有人对如何实现这一目标有一些意见吗?