[英]regex preg_replace php
我有隨機變量,如: Strip @ 489.000 Strip 1 @ 489.000 Strip 2 @ 589.000
我需要的輸出將是:'anything @' 489.000 之后的唯一數字
所以給我輸出:489.000 489.000 589.000
熱達到這個使用 php 正則表達式?
$string = ' Strip 1 @ 489.000'; $pattern = ' /(\s\S) @ (\d+)/i'; $replacement = '$3'; echo preg_replace($pattern, $replacement, $string);
要獲得所有匹配項,請使用
if (preg_match_all('/\S\s@\s+\K\d+(?:\.\d+)?/', $text, $matches)) {
print_r($matches[0]);
}
要獲得第一場比賽,請使用
if (preg_match('/\S\s@\s+\K\d+(?:\.\d+)?/', $text, $match)) {
print_r($match[0]);
}
細節
\\S
- 非空白字符\\s
- 一個空格@
- 一個@
字符\\s+
- 1+ 個空格\\K
- 匹配重置運算符\\d+
- 1+ 位數(?:\\.\\d+)?
- 一個點和 1+ 位數字的可選序列。請參閱正則表達式演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.