开发者

Regular Expression to fetch vertical offset from CSS

I have been fiddling with this for almost an hour and am getting nowhere. Regexes are not my strong suit.

I have data like the following:

.cke_button_about .cke_icon { background-position: 0 -76px; }
.cke_button_maximize .cke_icon { background-position: 0 -108px; }

I need to replace the vertical values (-76px and -108px) using preg_replace_c开发者_StackOverflowallback(). The callback function is written, but I can't for the life of me get the numbers out properly.

This works fine for 4-digit numbers:

preg_replace_callback("/(background\-position\:)(.*)(\d{4})(px)/", "recalculate",
$css_string);

but how can I make it so it recognizes any kind of number? {1-4} should work but somehow conflicts with the first, horizontal value.

Would anybody like to help me out?

Thanks in advance.


((-?)[0-9.]+) would probably be better, this would match the following cases:

  • 1.0
  • 1000
  • -400
  • 642.42
  • -642.42
  • 0

The whole code would be something like this:

preg_replace_callback("/(background\-position\:)(.*?)((-?)[0-9.]+)(px)/", "recalculate",
$css_string);


/(background\-position\:)(\D*\d*\D*)(\d+)(px)/

Gets what you want while preserving the current capturing groupings.


Can you do (background\-position)(.*)([0-9]+)(px)?


what about preg_replace_callback("/(background\-position\:)\s*(\d+)\s+(\d{4})(px)/", "recalculate", $css_string);? + meaning "one or more"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜