简体   繁体   中英

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,733,975 Rx

/ 132,882 RX mu uuu Au

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

I need to match the numbers before 'RX' or 'rX' or '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'.

any help would greatly be appreciated! solution may be posted in php code.

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.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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