简体   繁体   English

PHP正则表达式在文本前获取数字

[英]PHP Regular expression get numbers before text

could someone help me with my regular expression in php? 有人可以用我的正则表达式帮助我吗?

this is my regex: 这是我的正则表达式:

(?:\d*(\,)\d*)(\,\d*)?(\,\d*)?(\,\d*)?

this is my testdata: 这是我的测试数据:

1~ 2,906,730 RX 1〜2,906,730接收

1-.' 1-”。 2,733,975 Rx 2,733,975接收

/ 132,882 RX mu uuu Au / 132,882 RX mu uuu Au

'2(*__/ 182 rX ....212 '2(* __ / 182 rX .... 212

I need to match the numbers before 'RX' or 'rX' or 'Rx'. 我需要在“ RX”或“ rX”或“ Rx”之前匹配数字。 with above code the numbers in the last line don't get matched because it misses a ','. 使用上面的代码,最后一行中的数字不匹配,因为它错过了','。

I believe there is a better way to do this. 我相信有更好的方法可以做到这一点。 right now my regular expression doesn't even check if its followed by 'RX'. 现在,我的正则表达式甚至不检查其后跟“ RX”。

any help would greatly be appreciated! 任何帮助将不胜感激! solution may be posted in php code. 解决方案可能会发布在php代码中。

I think this will do it: 我认为这可以做到:

preg_match('/\d{1,3}(?:,\d{3}+)*(?= rx)/i', $data, $match);

?= is a positive lookahead -- it requires the previous regexp to be followed by this group. ?=是一个积极的前瞻 -它要求该组之前的则表达式。 And i makes the regexp case-insensitive, so it will match RX , rx , or any combination. 而且i使regexp不区分大小写,因此它将匹配RXrx或任何组合。

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM