簡體   English   中英

正則表達式:出現PHP之前的數值

[英]Regular Expressions: Numeric Value before Occurrence PHP

給定字符串:

100,000 this is some text 12,000 this is text I want to match.

我需要一個基於匹配的正則表達式來匹配12,000

text I want to match

因此,我們可以獲得以下職位:

strpos($haystack, 'text I want to match');

然后,我想我們可以使用正則表達式向后看:

但是,這是我需要幫助的地方。

很簡單:

/ ([0-9,]+) this is text I want to match\.$/

演示:

http://sandbox.onlinephpfunctions.com/code/b288ca9a322c7a5b54c6490334540ab142b6a979

如果您知道數字將始終位於要匹配的基礎上下文之前...

preg_match('/([\d,]+)\D*text I want to match/', $str, $match);
var_dump($match[1]);

另一個解決方案:

$re = "/([\\d,]+)(?=\\D*text I want to match)/";
$str = "100,000 this is some text 12,000 this is text I want to match.";

preg_match($re, $str, $matches);

現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM